feat: Implement authentication with JWT
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
f883dd1287
commit
c935f3f691
@@ -1,14 +1,33 @@
|
||||
import { Response, NextFunction } from 'express';
|
||||
import { rule, shield, allow } from 'graphql-shield';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import User from '../models/user';
|
||||
import RequestWithCurrentUser from '../types/express/request-with-current-user';
|
||||
import appConfig from '../config/app';
|
||||
|
||||
const authentication = async (req: RequestWithCurrentUser, _res: Response, next: NextFunction): Promise<void> => {
|
||||
// We set authentication to use the sample user we created temporarily.
|
||||
req.currentUser = await User.query().findOne({
|
||||
email: 'user@automatisch.com'
|
||||
}).throwIfNotFound();
|
||||
const isAuthenticated = rule()(async (_parent, _args, req) => {
|
||||
const token = req.headers['authorization'];
|
||||
|
||||
next()
|
||||
}
|
||||
if (token == null) return false;
|
||||
|
||||
try {
|
||||
const { userId } = jwt.verify(token, appConfig.appSecretKey) as {
|
||||
userId: string;
|
||||
};
|
||||
req.currentUser = await User.query().findById(userId).throwIfNotFound();
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
const authentication = shield({
|
||||
Query: {
|
||||
'*': isAuthenticated,
|
||||
},
|
||||
Mutation: {
|
||||
'*': isAuthenticated,
|
||||
login: allow,
|
||||
},
|
||||
});
|
||||
|
||||
export default authentication;
|
||||
|
@@ -1,18 +1,20 @@
|
||||
import { graphqlHTTP } from 'express-graphql';
|
||||
import graphQLSchema from '../graphql/graphql-schema'
|
||||
import graphQLSchema from '../graphql/graphql-schema';
|
||||
import logger from '../helpers/logger';
|
||||
import { applyMiddleware } from 'graphql-middleware';
|
||||
import authentication from '../helpers/authentication';
|
||||
|
||||
const graphQLInstance = graphqlHTTP({
|
||||
schema: graphQLSchema,
|
||||
schema: applyMiddleware(graphQLSchema, authentication),
|
||||
graphiql: true,
|
||||
customFormatErrorFn: (error) => {
|
||||
logger.error(error.path + ' : ' + error.message + '\n' + error.stack)
|
||||
logger.error(error.path + ' : ' + error.message + '\n' + error.stack);
|
||||
|
||||
return {
|
||||
message: error.message,
|
||||
locations: error.locations
|
||||
}
|
||||
}
|
||||
})
|
||||
locations: error.locations,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default graphQLInstance;
|
||||
|
Reference in New Issue
Block a user