This project is a simple authentication backend build with Node.js, Express, Json Web Token and MySQL database.
This project built with
Please follow all the steps below.
install the environment
- npm
npm install npm@latest -g
- Clone the repo
git clone https://github.com/andigaluh/auth-node-jwt-mysql.git
- Install NPM packages
npm install
- Create a database named authdb in your mysql server
- Enter your credential in
.env.template
and save as.env
- Start the project
node server.js
returns json data about sign up an user
-
URL
/api/auth/signup
-
Method:
POST
-
URL Params
Required:
None
-
Data Params
{ "username" : "admin", "email": "admin@domain.com", "password" : "password-here", "roles" : ["admin", "supervisor", "operator", "engineer"] }
-
Sample Call:
curl --location --request POST 'http://localhost:8080/api/auth/signup' \ --header 'Content-Type: application/json' \ --data-raw '{ "username" : "admin", "email": "admin@domain.com", "password" : "password-here", "roles" : ["admin","supervisor","operator","engineer"] }'
returns json data about login an user
-
URL
/api/auth/signin
-
Method:
POST
-
URL Params
Required:
None
-
Data Params
{ "username" : "admin", "password" : "password-here" }
-
Sample Call:
curl --location --request POST 'http://localhost:8080/api/auth/signin' \ --header 'Content-Type: application/json' \ --data-raw '{ "username" : "admin", "password" : "password-here" }'
returns json data about logout an user
-
URL
/api/auth/logout
-
Method:
POST
-
URL Params
Required:
None
-
Data Params
None
-
Sample Call:
curl --location --request POST 'http://localhost:8080/api/auth/logout' \ --header 'x-access-token: token-is-here'
returns json about data profile from user who is signed in
-
URL
/api/auth/me
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
None
-
Sample Call:
curl --location --request GET 'http://localhost:8080/api/auth/me' \ --header 'x-access-token: token-is-here'
returns json about update data profile from user who is signed in
-
URL
/api/auth/me
-
Method:
PUT
-
URL Params
Required:
None
-
Data Params
{ "username" : "username-edit" }
-
Sample Call:
curl --location --request PUT 'http://localhost:8080/api/auth/me' \ --header 'x-access-token: token-is-here' \ --header 'Content-Type: application/json' \ --data-raw '{ "username" : "username-edit" }'
returns json about change password from user who is signed in
-
URL
/api/auth/change-password
-
Method:
PUT
-
URL Params
Required:
None
-
Data Params
{ "password" : "password-edit" }
-
Sample Call:
curl --location --request PUT 'http://localhost:8080/api/auth/change-password' \ --header 'x-access-token: token-is-here' \ --header 'Content-Type: application/json' \ --data-raw '{ "password" : "password-edit" }'
returns json about check valid email for forget password
-
URL
/api/auth/check-valid-email
-
Method:
POST
-
URL Params
Required:
None
-
Data Params
{ "email" : "admin@domain.com" }
-
Sample Call:
curl --location --request POST 'http://localhost:8080/api/auth/check-valid-email' \ --header 'Content-Type: application/json' \ --data-raw '{ "email" : "admin@domain.com" }'
returns json about check valid encrypted key for reset password
-
URL
/api/auth/check-valid-email-enc
-
Method:
GET
-
URL Params
Required:
id=encrypted-code-is-here
-
Data Params
None
-
Sample Call:
curl --location --request GET 'http://localhost:8080/api/auth/check-valid-email-enc?id=encrypted-code-is-here'
returns json about reset password
-
URL
/api/auth/reset-password
-
Method:
PUT
-
URL Params
Required:
None
-
Data Params
{ "email" : "admin@domain.com", "password" : "new-password-here" }
-
Sample Call:
curl --location --request PUT 'http://localhost:8080/api/auth/reset-password' \ --header 'Content-Type: application/json' \ --data-raw '{ "email" : "admin@domain.com", "password" : "new-password-here" }'
returns json for public page
-
URL
/api/test/all
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
None
-
Sample Call:
curl --location --request GET 'http://localhost:8080/api/test/all'
returns json for user authorization
-
URL
/api/test/user
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
None
-
Sample Call:
curl --location --request GET 'http://localhost:8080/api/test/user' \ --header 'x-access-token: token-is-here'
returns json for supervisor authorization
-
URL
/api/test/mod
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
None
-
Sample Call:
curl --location --request GET 'http://localhost:8080/api/test/mod' \ --header 'x-access-token: token-is-here'
returns json for admin authorization
-
URL
/api/test/admin
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
None
-
Sample Call:
curl --location --request GET 'http://localhost:8080/api/test/admin' \ --header 'x-access-token: token-is-here'