Coder Social home page Coder Social logo

actix-auth-test's Introduction

User Authentication Service

This code comes from following the video series Building an Authentication Service using Actix (see author's code).

I didn't really modify anything here, as I was just using this as a learning experience.

The big learning for me here was:

  • the sqlx library is pretty cool. It's controversial to have the compiler connect to the database to infer schema, but it feels worth it to get true type safety checks on the database interactions. Having experienced one too many schema changes in the past blow up code I wasn't even thinking about at the time, this level of safety feels very helpful.
  • with enough supporting crates, Actix-web feels really productive and fluent. For new projects, I don't see any reason NOT to use Rust for APIs at this point. I agree with https://www.arewewebyet.org/ !
  • the error handling in Rust is just fantastic. It's so cool to see how color_eyre::Result can be used throughout just by tacking on the .context(...) to a native Result<U, T> type. In Go, this simple microservice would have its fair share of if err != nil hell.

All of the below notes are taken from the original repo's README, with minor modifications:

Usage

# SQLX CLI
cargo install --version=0.2.0 sqlx-cli

# Run docker-compose (postgres database)
docker-compose up

# Run migrations
sqlx mig run

# Run the server (http://localhost:3000)
cargo run

API

  • User Signup: POST /signup
    curl --request POST \
      --url http://localhost:3000/signup \
      --header 'content-type: application/json' \
      --data '{
          "username": "user1",
          "email": "[email protected]",
          "password": "user1pass"
      }'
    
  • Auth: POST /auth
    curl --request POST \
      --url http://localhost:3000/auth \
      --user user1
    
  • User profile: GET /me
    curl --request GET \
    --url http://localhost:3000/me \
    --header 'authorization: Bearer <jwt_token>'
    
  • Update profile: PUT /me
    curl --request POST \
      --url http://localhost:3000/me \
      --header 'authorization: Bearer <jwt_token>' \
      --header 'content-type: application/json' \
      --data '{
          "full_name": "User One"
      }'
    
  • Example jwt token (debug it in JWT.io):
    eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhNzM1Mjk0Yi04OWNhLTQ0NzktYTRjMi05NDhmNzE3OWRkZjAiLCJleHAiOjE2MDczNjgwNzN9.UrxB3bx5kH0hQ45MuTb8AqJZWRZuJyuIXvUVPS9IjvY
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.