Skip to content

Commit

Permalink
Modification in the index.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
JustineRobert committed Nov 2, 2019
1 parent 14d2e9d commit 67fea9f
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions nodeauthapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ app.get('/api', (req, res) => {
});

app.post('/api/posts', verifyToken, (req, res) =>{
res.json({
message: 'Post Created Successfully'
jwt.verify(req.token, 'secretkey', (err, authData) =>{
if(err) {
res.sendStatus(403);
}else{
res.json({
message: 'Post Created Successfully',
authData
});
}
});
});
/*

app.post('/api/login', (req, res) => {
//Mock user
const user{
Expand All @@ -29,12 +36,12 @@ app.post('/api/login', (req, res) => {
address: 'Kampala, Uganda'
}

jwt.sign({user}, 'secretkey', (err, token) =>{
jwt.sign({user}, 'secretkey', {expiresIn: '50s'}, (err, token) =>{
res.json({
token
});
});
}); */
});

//TOKEN FORMAT
//Authorization: Bearer <access_token>
Expand All @@ -46,12 +53,19 @@ function verifyToken(req, res, next) {
const bearerHeader = req.headers['authorization'];
//Check if bearer is undefined
if(typeof bearerHeader !== 'undefined') {

//Split at the space
const bearer = bearerHeader.split(' ');
//Get token from Array
const bearerToken = bearer[1];
//Set the token
res.token = bearerToken;
//Call next middleware
next();
}else {
//Forbidden
res.sendStatus(403);
}
}


app.listen(5000, ()=>console.log('Server is running on port 5000'));
app.listen(5000, ()=>console.log('Server is successfully running on port 5000'));

0 comments on commit 67fea9f

Please sign in to comment.