Git Product home page Git Product logo

go-authentication-api's Introduction

go-authentication-api

A simple authentication API.

What you can do:

  • add new users
  • sign in with your user when is already registered
  • get a JWT if authentication is successful

Running Locally

git clone https://github.com/Edmartt/go-authentication-api

or ssh instead:

[email protected]:Edmart/go-authentication-api
cd go-authentication-api
go mod tidy
go run main.go

Using

Note

This is actually set for using with SQLite for simplicity, but you can switch this database manager for postgres if needed. Just go to internal/users/data/repository.go and change the line 10 typing Postgres. If you use postgres, remember to set the .env.example values according to your setup. Note that I left the : as part of HTTP_PORT env var value, so if you need to change this port just delete the numbers.

You can use any client you want. I'm using curl:

curl -i -X POST -H "Content-Type: application/json" -d '{"username":"shinigami", "password":"12345678"}' http://<host>:<port>/api/v1/public/signup

Response:

HTTP/1.1 201 Created
Content-Type: application/json
Date: Sun, 29 May 2022 06:24:41 GMT
Content-Length: 26

{"status":"User Created"}

Once the user is created you can sign in:

curl -i -X POST -H "Content-Type: application/json" -d '{"username":"shinigami", "password":"12345678"}' http://<host>:<port>/api/v1/public/login

Response:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 29 May 2022 06:25:17 GMT
Content-Length: 176

{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTM4MDU4MTcsImlzcyI6IlNhbSBTZXBpb2wiLCJBdHRyaWJ1dGUiOiJzaGluaWdhbWkifQ.CdEL0FqZxOHAit5C6zfpcX2HuhLESDpwcKQSzlowm2s"}

After obtaining the token, we can send it to a special endpoint that will give us the user's data as a response.

curl -i -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTM4MDU4MTcsImlzcyI6IlNhbSBTZXBpb2wiLCJBdHRyaWJ1dGUiOiJzaGluaWdhbWkifQ.CdEL0FqZxOHAit5C6zfpcX2HuhLESDpwcKQSzlowm2s" http://<host:port>/a
pi/v1/private/users/user

Response

HTTP/1.1 200 OK
Content-Type: application/json
Date: Sun, 29 May 2022 17:44:38 GMT
Content-Length: 201

{"id":"02a542e5-fbd8-46ae-b0dd-7043bb226c9f","username":"shinigami","password":"","ID":0,"CreatedAt":"2022-05-05T07:25:37.560142-05:00","UpdatedAt":"2022-05-05T07:25:37.560142-05:00","DeletedAt":null}

Note: The password is already hashed but even like that, we don't want to expose our password hash

Running Unit Test

go test -v -coverprofile=coverage.out ./... ./...

Showing Coverage in Browser

go tool cover -html=coverage.out

DEMO

A demo application is deployed here:

go-authentication-api's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

go-authentication-api's Issues

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.