-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-server.js
More file actions
56 lines (47 loc) · 1.08 KB
/
Copy pathjson-server.js
File metadata and controls
56 lines (47 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const jsonServer = require('json-server')
const server = jsonServer.create()
const middlewares = jsonServer.defaults()
server.use(jsonServer.bodyParser);
server.use(middlewares);
const testUser = {
email: 'feyyaz@test.com',
password: '1234'
};
const testProfile = {
email: 'feyyaz@test.com',
firstName: 'Feyyaz',
lastName: 'Akkuş'
};
const testToken = 'example-token';
const authUser = (req) => {
return req.body.email === testUser.email && req.body.password === testUser.password;
}
server.post('/auth', (req, res) => {
if (authUser(req)) {
res.status(200).json({
success: true,
token: testToken,
profile: testProfile
});
} else {
res.status(401).json({
success: false,
error: 'Email or password incorrect'
});
}
});
server.post('/register', (req, res) => {
res.status(200).json({
success: true
});
});
server.get('/profile', (req, res) => {
console.log(req.headers);
res.status(200).json({
success: true,
profile: testProfile
});
});
server.listen(5000, () => {
console.log('JSON Server is running..');
});