Authentication system using Python (Django) and SvelteKit
Tags: githubhack23, python, django, svelte
Published: 2023-05-15 19:52:40

## What I built

A performant, secure and reliable session-based authentication system with Django backend and SvelteKit frontend. It has a CI enabled by GitHub actions.

### Category Submission

DIY Deployments

### App Link

Its live version is [here](https://rust-auth.vercel.app/).

### Screenshots

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnx1otc8b4cazb42vkke.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcqrou0uk7wsg3cxymnm.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkpwx0myd1y34zm3cuwn.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ix0dtffcmhkydmv858t.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tegvvah28nknhkkxikm9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nudygep73983szgyd8t1.png)

### Description

This app uses minimal dependencies (pure Django - no REST API framework) to build a secure, performant and reliable (with 100% automated test coverage, enforced static analysis using Python best uniform code standards) session-based authentication REST APIs which were then consumed by a SvelteKit-based frontend Application.

Users' profile images are uploaded directly to AWS S3 (in tests, we ditched S3 and used Django's [InMemoryStorage](https://docs.djangoproject.com/en/4.2/ref/files/storage/#django.core.files.storage.InMemoryStorage) for faster tests).

A custom password reset procedure was also incorporated, and Celery tasks did email sendings.

The front end is resilient as it can mostly work without JavaScript. The user session is persisted until it is destroyed and the experience gives a hybrid feel of a SPA and MPA (termed #transitionalApps by Rich Haris).

### Link to Source Code

It's hosted on GitHub via [django-auth-backend](https://github.com/Sirneij/django-auth-backend)

{% github Sirneij/django-auth-backend %}

### Permissive License

MIT

## Background (What made you decide to build this particular app? What inspired you?)

A few days ago, I shared the [series](https://dev.to/sirneij/series/22690) I wrote on building an authentication system using rust (actix-web) and sveltekit with my LinkedIn connections. One of my connections [requested that I make a Django version](https://www.linkedin.com/feed/update/urn:li:activity:7062762790607831040?commentUrn=urn%3Ali%3Acomment%3A%28activity%3A7062762790607831040%2C7062803465449664512%29). I obliged and here it is!

### How I built it (How did you utilize GitHub Actions or GitHub Codespaces? Did you learn something new along the way? Pick up a new skill?)

The backend service was built using Django with PostgreSQL database, Redis for session storage, and AWS S3 for file storage. The APIs were built without the use of external REST API frameworks such as [Django REST framework](https://www.django-rest-framework.org/). Data serialization and JSON responses were manually handled. Most of the views were made asynchronous. For testing, pytest and its ecosystem were heavily used. [Mypy](https://www.mypy-lang.org/), [Pylint](https://pylint.org/) and others were used for Static analysis. GitHub Actions were used for automated testing, coverage report and static analysis.

I learnt quite a few things:

- Writing Django views with the `async` keyword and handling synchronous code in them
- Manually handling and parsing multipart form data in Django view using the scantily documented (if even documented) [MultiPartParser](https://github.com/django/django/blob/main/django/http/multipartparser.py).
- Setting up and enforcing standard Python code styles with mypy, pylint, prospector, pep8, pyflakes and a host of other tools.
- Manually encoding form data, creating uploading and deleting mock files during tests
- Setting up and fixing issues with GitHub actions for Django applications and using them to automatically test and analyze apps against different Python versions

### Additional Resources/Info

This application is the Python (Django) version of [this series](https://dev.to/sirneij/series/22690). A series of articles will be written to explain how it was built. Watch out.