A simple authentication flow using Rust and Actix-web, with a PostgreSQL database and a sveltekit frontend.

Overview

Rust-auth-example

This repository aims to represent a simple authentication flow using Rust and Actix-web, with a PostgreSQL database and a sveltekit frontend.

The Goal

The goal of this repository is to provide a simple example of how to implement session-based authentication in Rust & Actix. This repository will be used as a reference for future projects. This repository will also be used as a reference for future blog posts.

This repository also aims to provide examples of how to use the following technologies:

  • Diesel
  • Actix
  • Juniper
  • Sveltekit
  • Tailwind
  • DaisyUI

As well as provide examples of idiomatic Rust practices.

This repository aims to grow over time to include more features and more technologies that are commonly used in web stacks and Rust projects.

The project consists of a actix-hosted static sveltekit SPA while still maintaining the Vite development server for better development experience. See the 'How to run' and 'Developement' sections below

The Stack

The Backend

  • Diesel will be used as an ORM to interact with the database.
    • Argon2 will be used to hash passwords.
  • Actix web will be used as a web framework.
    • Actix-session will be used to manage sessions.
    • Actix-cors will be used to manage CORS.
  • Juniper will be used as a GraphQL framework.

The Frontend

  • Sveltekit will be used as a frontend framework.
  • Tailwind will be used as a CSS framework.
  • DaisyUI will be used for additional tailwind components.
  • We will use normal fetch requests to interact with the backend.

How to run

Docker

  • Install Docker
  • Run docker build -t rust-actix-auth-flow-example . to build the image
  • run docker run -p 8000:8000 rust-actix-auth-flow-example:latest to run the image and expose the port
  • The backend will be running on localhost:8000 and will statically serve the frontend on the same port
  • head to localhost:8000 to see the frontend and interact with the backend

Alternatively you can run the backend and frontend separately and set up the database manually

Database

  • Install PostgreSQL
  • Create a database named rust-auth-example
  • Create a superuser(to avoid permission issues) named example with (optionally) no password.
  • update the env file with the correct database credentials in the case where you want a custom username and password and database name

Frontend

  • Install Node.js
  • Run npm install to install the dependencies
  • Run npm build to build the frontend
  • The frontend will be statically hosted by the backend so they have the same domain

Backend

  • Install Rust and Cargo
  • Install Diesel CLI (cargo install diesel_cli --no-default-features --features postgres)(make sure you have the postgres-libs installed)
  • Cookie same site is set to strict when running in production mode.
  • Run ENV=prod cargo run --release to run the backend, the migrations will automatically apply.
  • The backend will be running on localhost:8000
  • head to 'localhost:8000' to interact with the frontend

How to use

Frontend

  • The frontend is a simple login and register page.
  • The frontend will send a request to the backend to register a user.
  • The frontend will send a request to the backend to login a user.
  • The frontend will send a request to the backend to get the current user.
  • The frontend will send a request to the backend to logout a user.

Backend

  • The backend will handle the requests from the frontend.
  • The backend will register a user.
  • The backend will login a user.
  • The backend will get the current user.
  • The backend will logout a user.

Development

There are some weird issues with vite and firefox, so I recommend using chrome for development. I will try to fix this in the future.

Database

  • Install PostgreSQL
  • Create a database named rust-auth-example
  • Create a superuser(to avoid permission issues) named example with (optionally) no password.
  • update the env file with the correct database credentials in the case where you want a custom username and password and database name

Backend

  • Install Rust and Cargo
  • Install Diesel CLI (cargo install diesel_cli --no-default-features --features postgres)(make sure you have the postgres-libs installed)
  • set up the DATABASE_URL environment variable in the .env file to point to the database
  • Run diesel migration run to run the migrations
  • Run cargo run to run the backend
  • The backend will be running on localhost:8000
  • Cookie security is set to lax different when running in development mode

Frontend

  • Install Node.js
  • Run npm install to install the dependencies
  • Run npm run dev to run vite and start a development server
  • it will be running on localhost:5173
  • Requests will automatically be routed to the backend on localhost:8000
You might also like...
RESTful Todo API with Actix-web and SeaORM. Documented by swagger-ui

RESTful Todo RESTful Todo API with Actix and SeaORM. Documented by swagger-ui Prerequisites Rust Usage Clone the repository and run the following comm

Rust HTTP API Template using PostgreSQL, Redis, RabbitMQ, and Hexagonal Architecture

Rust Template HTTP API Rust API Template using PostgreSQL, Redis, RabbitMQ, and Hexagonal Architecture The following template provides a basic structu

JWT Authentication in Rust using Axum Framework

Are you interested in building a secure authentication system for your Rust web application? Look no further than the Axum framework and JSON Web Tokens (JWTs)! Axum is a fast and scalable Rust web framework that provides a reliable and efficient platform for developing microservices and APIs.

Code template for a production Web Application using Axum: The AwesomeApp Blueprint for Professional Web Development.

AwesomeApp rust-web-app More info at: https://awesomeapp.dev/rust-web-app/ rust-web-app YouTube episodes: Episode 01 - Rust Web App - Course to Produc

Quick demo of a REST frontend with a Redis session store.

axum-rest-starter-example Important Tasks Ensure session UUID is unique Protect /api/ with JWT Add CSRF CORS? Dev Setup (1) Run docker compose up to f

A Rust GraphQL system with full support for subscriptions and authentication that works out of the box.

Diana is a GraphQL system for Rust that's designed to work as simply as possible out of the box, without sacrificing configuration ability.

A Rust crate for managing authentication and authorization with support for multi-tenant / B2B products, powered by PropelAuth

PropelAuth Add authentication and authorization to your application. This library is meant to be used with a PropelAuth account. You can sign up and g

🪪 Session-based user authentication for Axum.

axum-login 🪪 Session-based user authentication for Axum. 🎨 Overview axum-login is a Tower middleware providing session-based user authentication for

Axum + JWT authentication Middleware that allows you to start building your application fast

axum_jwt_ware Integration Guide Simple Axum + JWT authentication middleware with implemented Login and refresh token. Goal I aim to simplify the proce

Owner
Kival Mahadew
I do programming stuff, and sometimes, it works!
Kival Mahadew
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

Actix 16.2k Jan 2, 2023
Noria: data-flow for high-performance web applications

Noria: data-flow for high-performance web applications Noria is a new streaming data-flow system designed to act as a fast storage backend for read-he

MIT PDOS 4.5k Dec 28, 2022
Web Application with using Rust(Actix, Diesel and etc)

Santa Service App Used technology stack Web Server with using Rust (Actix, Actix-web, Diesel) Data base (Postgres) Console Application (Tokio) Tasks o

null 3 Jan 8, 2023
An API project using Rust, Actix Web and JWT. *WIP*

Actix-web REST API with JWT (WIP) A simple CRUD backend app using Actix-web, Diesel and JWT Require Rust Stable Postgres Or using Docker How to run Ma

Akhil Sharma 4 Sep 21, 2023
example of a full stack web app (backend and frontend) wrtiten in Rust

rust-fullstack-example An example of creating a full stack web application (backend and frontend) using Rust. Backend Go to ./backend and start the se

mario 41 Dec 16, 2022
Perseus is a blazingly fast frontend web development framework built in Rust with support for major rendering strategies

Perseus is a blazingly fast frontend web development framework built in Rust with support for major rendering strategies, reactivity without a virtual DOM, and extreme customizability

arctic_hen7 1.2k Jan 8, 2023
A GUI frontend in Rust based on web-view

neutrino I am not working anymore on this project. If you want to become a maintainer of neutrino, please answer to this issue. Preamble Docs | Repo |

null 287 Nov 25, 2022
Implementation of the RealWorld backend API spec in Actix, Rust's powerful actor system and most fun web framework.

Actix codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API. ❗ (2021/05/13) This cod

Allen 475 Jan 2, 2023
A (flash) message framework for actix-web. A port to Rust of Django's message framework.

actix-web-flash-messages Flash messages for actix-web Web applications sometimes need to show a one-time notification to the user - e.g. an error mess

Luca Palmieri 31 Dec 29, 2022
A newsletter with actix-web and sqlx-postgres

Newsletter backend Health check: production Pre-requisites You'll need to install: Rust Docker There are also some OS-specific requirements. Windows c

Nadeem Bhati 4 Dec 10, 2022