A web server backend with complete JWT user authentication, written in GO.
- GO: programming language
- JWT: authentication strategy
- PostgreSQL: primary database
- Render: cloud hosting platform
Before you can run the server locally, you need to create a .env file which stores most of the server's private configurations. An example env file shows all the parameters required, then the server can be run by using the command:
go run server.go
Alternatively, you can batch execute some pre-commands and run the server at once using make.
Installation (Unix):
sudo apt update
sudo apt install make
Then run the server using:
make server
- domain
/
- domain
/auth/sign-up
- domain
/auth/sign-in
- domain
/auth/sign-out
- domain
/auth/oauth/google
- domain
/user/id
Note
The URL and port number can be different depending on your configurations.
All sign-up requests to the server follow this convention.
[POST] http://localhost:3000/auth/sign-up
{
"email": "root@usr.ssh",
"username": "root",
"password": "rootsystemuser"
}
The provided password is hashed on the server. Upon successful sign-up, a response like the one below will be sent along with a token stored in the client's cookie store.
{
"message": "Successfully inserted user into database",
"success": true,
"payload": {
"id": "d7407d4c-74d2-4f83-9298-99ac81565716",
"username": "root",
"email": "root@usr.ssh"
}
}
Sign-in requests made to the server should follow this format.
[POST] http://localhost:3000/auth/sign-in
{
"email": "root@usr.ssh",
"password": "rootsystemuser"
}
On successful sign-in, the user object is returned along with a JSON Web Token for future authentication.
A successful sign-in response looks like this:
{
"message": "Successfully signed-in",
"success": true,
"payload": {
"id": "d7407d4c-74d2-4f83-9298-99ac81565716",
"username": "user",
"email": "user@code.sh"
}
}
The user can also sign-in with their Google accounts using OAuth
[GET] http://localhost:3000/auth/oauth/google
A successful sign-in response looks like this:
{
"message": "Successfully signed-in with Google",
"success": true,
"payload": {
"id": "d7407d4c-74d2-4f83-9298-99ac81565716",
"username": "user",
"email": "user@code.sh"
}
}
Sign-out requests expire the cookie and revokes user authorization.
[POST] http://localhost:3000/auth/sign-out
A successful sign-in response looks like this:
{
"message": "Successfully signed-out",
"success": true,
"payload": null
}