-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
2 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +0,0 @@ | ||
const checkToken=function(req,res,next){ | ||
let token=req.headers['authorization'] | ||
|
||
if (token){ | ||
// In practice use json web token verify method to check if token is valid | ||
// when next is called in middleware, it will run the next function | ||
next() | ||
} else { | ||
res.json({ | ||
success:false, | ||
message:'no token in headers' | ||
}) | ||
} | ||
} | ||
|
||
module.exports= { | ||
checkToken:checkToken | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,5 @@ | |
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.17.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +0,0 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
const bodyParser = require('body-parser'); | ||
const middleware = require('./middleware.js'); | ||
|
||
app.use(bodyParser.json()) | ||
|
||
const mockUserData=[ | ||
{name:'Mark'}, | ||
{name:'Jill'} | ||
] | ||
|
||
app.get('/users',function(req,res){ | ||
res.json({ | ||
success: true, | ||
message: 'successfully got users. Nice!', | ||
users: mockUserData | ||
}) | ||
}) | ||
// colons are used as variables that be viewed in the params | ||
app.get('/users/:id',function(req,res){ | ||
console.log(req.params.id) | ||
}) | ||
|
||
app.post('/login',function(req,res){ | ||
// Typically passwords are encrypted using something like bcrypt before sending to database | ||
const username=req.body.username; | ||
const password=req.body.password; | ||
|
||
// This should come from the database | ||
const mockUsername="billyTheKid"; | ||
const mockPassword="superSecret"; | ||
|
||
if (username===mockUsername && password===mockPassword){ | ||
// In practice, use JSON web token sign method here to make an encrypted token | ||
res.json({ | ||
success: true, | ||
message: 'password and username match!', | ||
token: 'encrypted token goes here' | ||
}) | ||
} else { | ||
res.json({ | ||
success: false, | ||
message: 'password and username do not match' | ||
}) | ||
} | ||
|
||
}) | ||
// admin route is protected by checking token in middleware | ||
app.get('/admin',middleware.checkToken,function(req,res){ | ||
|
||
res.json({ | ||
success:true, | ||
message:'admin authorized', | ||
adminData: 'secure data from database' | ||
}) | ||
|
||
}) | ||
|
||
app.listen(8000,function(){console.log('server is listening')}) | ||