-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
27 lines (21 loc) · 706 Bytes
/
example.js
File metadata and controls
27 lines (21 loc) · 706 Bytes
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
const jwt = require('jsonwebtoken');
const User = require('../models/User');
/**
* Fetch data with a valid JWT this is an example of how to
* limit access to authenticated users. We simply use their
* user ID that's been stored in the JWT
*
* @param {*} data Requires token field with a valid token
* @returns {Object} { message: string } or error
*/
exports.getData = async(data) => {
if (!data.token) return { error: 'No token provided' };
let _user;
try {
_user = jwt.verify(data.token, process.env.JWT_SECRET);
} catch(err) {
return { error: err.message };
}
const user = await User.findById(_user._id);
return { message: `Authenticated as ${user.username}!` };
};