Updated profile and post capabilities
This commit is contained in:
@@ -36,16 +36,36 @@ function signToken(userId) {
|
||||
return { token: `${content}.${signature}`, expiresAt: payload.exp };
|
||||
}
|
||||
|
||||
function authenticateToken(req, res, next) {
|
||||
function getTokenFromRequest(req) {
|
||||
const authorization = req.get('authorization') || '';
|
||||
const match = authorization.match(/^Bearer\s+([^\s]+)$/i);
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
|
||||
if (!match) {
|
||||
return res.status(401).send('A valid Bearer token is required');
|
||||
const cookieHeader = req.get('cookie') || '';
|
||||
if (cookieHeader) {
|
||||
const cookies = cookieHeader.split(';');
|
||||
for (const cookie of cookies) {
|
||||
const [name, ...rest] = cookie.trim().split('=');
|
||||
if (name === 'fc_session_token') {
|
||||
return rest.join('=');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function authenticateToken(req, res, next) {
|
||||
const token = getTokenFromRequest(req);
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).send('A valid Bearer token or session cookie is required');
|
||||
}
|
||||
|
||||
try {
|
||||
const [encodedHeader, encodedPayload, providedSignature] = match[1].split('.');
|
||||
const [encodedHeader, encodedPayload, providedSignature] = token.split('.');
|
||||
if (!encodedHeader || !encodedPayload || !providedSignature) {
|
||||
throw new Error('Malformed token');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user