Skip to content

Commit 5a1751c

Browse files
author
shubhankar
committed
Add register functionality
1 parent ac7289d commit 5a1751c

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# JSONServer + JWT Auth
22

3-
A Fake REST API using json-server with JWT authentication. You can find the [complete tutorial here](https://www.techiediaries.com/fake-api-jwt-json-server/)
3+
A Fake REST API using json-server with JWT authentication.
4+
5+
Implemented End-points: login,register
46

57
## Install
68

@@ -14,12 +16,13 @@ Might need to run
1416
npm audit fix
1517
```
1618

17-
## How to login?
19+
## How to login/register?
1820

19-
You can login by sending a POST request to
21+
You can login/register by sending a POST request to
2022

2123
```
22-
POST http://localhost:3000/auth/login
24+
POST http://localhost:8000/auth/login
25+
POST http://localhost:8000/auth/register
2326
```
2427
with the following data
2528

server.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,49 @@ function isAuthenticated({email, password}){
3030
return userdb.users.findIndex(user => user.email === email && user.password === password) !== -1
3131
}
3232

33+
// Register New User
34+
server.post('/auth/register', (req, res) => {
35+
console.log("register endpoint called; request body:");
36+
console.log(req.body);
37+
const {email, password} = req.body;
3338

34-
server.post('/auth/login', (req, res) => {
39+
if(isAuthenticated({email, password}) === true) {
40+
const status = 401;
41+
const message = 'Email and Password already exist';
42+
res.status(status).json({status, message});
43+
return
44+
}
45+
46+
fs.readFile("./users.json", (err, data) => {
47+
if (err) {
48+
return console.error(err);
49+
};
50+
51+
var data = JSON.parse(data.toString());
52+
53+
var last_item_id = data.users[data.users.length-1].id;
3554

36-
console.log("login");
55+
data.users.push({id: last_item_id + 1, email: email, password: password}); //add some data
56+
var writeData = fs.writeFile("./users.json", JSON.stringify(data), (err, result) => { // WRITE
57+
if (err) {
58+
return console.error(err);
59+
} else {
60+
console.log(result);
61+
console.log("Success");
62+
}
63+
});
64+
});
65+
66+
const access_token = createToken({email, password})
67+
console.log("Access Token:" + access_token);
68+
res.status(200).json({access_token})
69+
})
70+
71+
// Login to one of the users from ./users.json
72+
server.post('/auth/login', (req, res) => {
73+
console.log("login endpoint called; request body:");
3774
console.log(req.body);
38-
const {email, password} = req.body
75+
const {email, password} = req.body;
3976
if (isAuthenticated({email, password}) === false) {
4077
const status = 401
4178
const message = 'Incorrect email or password'
@@ -64,7 +101,6 @@ server.use(/^(?!\/auth).*$/, (req, res, next) => {
64101
res.status(status).json({status, message})
65102
return
66103
}
67-
68104
next()
69105
} catch (err) {
70106
const status = 401

users.json

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1 @@
1-
{
2-
"users": [
3-
{
4-
"id": 1,
5-
"name": "bruno",
6-
"email": "bruno@email.com",
7-
"password": "bruno"
8-
},
9-
{
10-
"id": 2,
11-
"name": "techie",
12-
"email": "techie@email.com",
13-
"password": "techie"
14-
},
15-
{
16-
"id": 3,
17-
"name": "nilson",
18-
"email": "nilson@email.com",
19-
"password": "nilson"
20-
}
21-
]
22-
}
1+
{"users":[{"id":1,"email":"bruno@email.com","password":"bruno"},{"id":2,"email":"techie@email.com","password":"techie"},{"id":3,"email":"nilson@email.com","password":"nilson"},{"id":4,"email":"nilson1@email.com","password":"nilson"},{"id":5,"email":"nilson2@email.com","password":"nilson"},{"id":6,"email":"nilson3@email.com","password":"nilson"},{"id":7,"email":"nilson4@email.com","password":"nilson"},{"id":8,"email":"nilson7@email.com","password":"nilson"}]}

0 commit comments

Comments
 (0)