apollo-server-express#AuthenticationError TypeScript Examples
The following examples show how to use
apollo-server-express#AuthenticationError.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: service.ts From one-platform with MIT License | 6 votes |
context = ({ req, connection }: any) => {
const authorizationHeader = req?.headers?.authorization || connection?.context?.Authorization;
if ( !authorizationHeader ) {
throw new AuthenticationError( 'Auth Token Missing' );
}
if ( !authorizationHeader.toLowerCase().startsWith( 'bearer' ) ) {
throw new AuthenticationError( 'Only Bearer tokens are supported' );
}
const token = authorizationHeader.split( ' ' )[ 1 ];
if ( uuidValidate( token ) ) {
return verifyAPIKey(token)
.then((res) => ({ uid: res._id, roles: res.roles, scopes: res.scopes, token }))
.catch((err) => {
throw new AuthenticationError(err.message);
});
} else {
return verifyJwtToken( token, ( err: any, payload: any ) => {
if ( err ) {
throw new AuthenticationError( err.message );
}
return { uid: payload.rhatUUID, roles: payload.role, scope: payload.scope?.split(' '), token };
} );
}
}
Example #2
Source File: auth-checker.ts From convoychat with GNU General Public License v3.0 | 6 votes |
useAuth: AuthChecker<Context> = (
{ root, args, context, info },
_roles
) => {
// here we can read the user from context
// and check his permission in the db against the `roles` argument
// that comes from the `@Authorized` decorator, eg. ["ADMIN", "MODERATOR"]
if (!context.getUser()) {
throw new AuthenticationError("User not authenticated");
}
return true;
}
Example #3
Source File: apollo-server-resolver-adapter.ts From clean-ts-api with GNU General Public License v3.0 | 6 votes |
adaptResolver = async (controller: Controller, args?: any, context?: any): Promise<any> => {
const request = {
...(args || {}),
accountId: context?.req?.accountId
}
const httpResponse = await controller.handle(request)
switch (httpResponse.statusCode) {
case 200:
case 204: return httpResponse.body
case 400: throw new UserInputError(httpResponse.body.message)
case 401: throw new AuthenticationError(httpResponse.body.message)
case 403: throw new ForbiddenError(httpResponse.body.message)
default: throw new ApolloError(httpResponse.body.message)
}
}