A loopback-next extension for authentication feature. Oauth strategies supported.
Repo sourcefuse/loopback4-authentication, read from GitHub 10 min ago. sourcefuse has not joined Take My Repo.
sourcefuse/loopback4-authentication is a public repo on GitHub by sourcefuse. A loopback-next extension for authentication feature. Oauth strategies supported. It declares the MIT license on GitHub. It is written mainly in TypeScript, has 74 stars and last changed on 2026-11-19. Nobody has reviewed it on Take My Repo yet, which says nothing about its quality. Read from GitHub on 2026-11-23.
In the maintainer's words. Shown from GitHub; Take My Repo does not keep a copy.
This is a loopback-next extension for adding authentication layer to a REST application in loopback 4. This extension is based on the implementation guidelines provided on official @loopback/authentication page. It provides support for seven passport based strategies.
You can use one or more strategies of the above in your application. For each of the strategy (only which you use), you just need to provide your own verifier function, making it easily configurable. Rest of the strategy implementation intricacies is handled by extension.
npm install loopback4-authentication
For a quick starter guide, you can refer to our loopback 4 starter application which utilizes all of the above auth strategies from the extension in a simple multi-tenant application. Refer to the auth module there for specific details on authentication.
The first and common step for all of the startegies is to add the component to the application. See below
// application.ts export class ToDoApplication extends BootMixin( ServiceMixin(RepositoryMixin(RestApplication)), ) { constructor(options: ApplicationConfig = {}) { super(options); // Set up the custom sequence this.sequence(MySequence); // Set up default home page this.static('/', path.join(__dirname, '../public')); // Add authentication component this.component(AuthenticationComponent); // .... Rest of the code below } }
Once this is done, you are ready to configure any of the available strategy in the application.
First, create an AuthClient model implementing the IAuthClient interface. The purpose of this model is to store oauth registered clients for the app in the DB. See sample below.
@model({ name: 'auth_clients', }) export class AuthClient extends Entity implements IAuthClient { @property({ type: 'number', id: true, }) id?: number; @property({ type: 'string', required: true, name: 'client_id', }) clientId: string; @property({ type: 'string', required: true, name: 'client_secret', }) clientSecret: string; @property({ type: 'array', itemType: 'number', name: 'user_ids', }) userIds: number[]; constructor(data?: Partial<AuthClient>) { super(data); } }
Create CRUD repository for the above model. Use loopback CLI.
lb4 repository
Add the verifier function for the strategy. You need to create a provider for the same strategy. You can add your application specific business logic for client auth here. Here is simple example.
import {Provider} from '@loopback/context'; import {repository} from '@loopback/repository'; import {VerifyFunction} from 'loopback4-authentication'; import {AuthClientRepository} from '../../../repositories'; export class ClientPasswordVerifyProvider implements Provider<VerifyFunction.OauthClientPasswordFn> { constructor( @repository(AuthClientRepository) public authClientRepository: AuthClientRepository, ) {} value(): VerifyFunction.OauthClientPasswordFn { return async (clientId, clientSecret, req) => { return this.authClientRepository.findOne({ where: { clientId, clientSecret, }, }); }; } }
Please note the Verify function type VerifyFunction.OauthClientPasswordFn.
Now bind this provider to the application in application.ts.
import {AuthenticationComponent, Strategies} from 'loopback4-authentication';
// Add authentication component this.component(AuthenticationComponent); // Customize authentication verify handlers this.bind(Strategies.Passport.OAUTH2_CLIENT_PASSWORD_VERIFIER).toProvider( ClientPasswordVerifyProvider, );
Finally, add the authenticate function as a sequence action to sequence.ts.
export class MySequence implements SequenceHandler { constructor( @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, @inject(SequenceActions.SEND) public send: Send, @inject(SequenceActions.REJECT) public reject: Reject, // Inject CLIENT_AUTH_ACTION sequence action provider @inject(AuthenticationBindings.CLIENT_AUTH_ACTION) protected authenticateRequestClient: AuthenticateFn<AuthClient>, ) {} async handle(context: RequestContext) { try { const {request, response} = context; const route = this.findRoute(request); const args = await this.parseParams(request, route); request.body = args[args.length - 1]; // Perform client authentication here await this.authenticateRequestClient(request); const result = await this.invoke(route, args); this.send(response, result); } catch (err) { this.reject(context, err); } } }
After this, you can use decorator to apply auth to controller functions wherever needed. See below.
@authenticateClient(STRATEGY.CLIENT_PASSWORD, { passReqToCallback: true }) @post('/auth/login', { responses: { [STATUS_CODE.OK]: { description: 'Auth Code', content: { [CONTENT_TYPE.JSON]: Object, }, }, }, }) async login( @requestBody() req: LoginRequest, ): Promise<{ code: string; }> { .... }
For accessing the authenticated AuthClient model reference, you can inject the CURRENT_CLIENT provider, provided by the extension, which is populated by the auth action sequence above.
@inject.getter(AuthenticationBindings.CURRENT_CLIENT) private readonly getCurrentClient: Getter<AuthClient>,
First, create a AuthUser model implementing the IAuthUser interface. You can implement the interface in the user model itself. See sample below.
@model({ name: 'users', }) export class User extends Entity implements IAuthUser { @property({ type: 'number', id: true, }) id?: number; @property({ type: 'string', required: true, name: 'first_name', }) firstName: string; @property({ type: 'string', name: 'last_name', }) lastName: string; @property({ type: 'string', name: 'middle_name', }) middleName?: string; @property({ type: 'string', required: true, }) username: string; @property({ type: 'string', }) email?: string; @property({ type: 'string', }) password?: string; constructor(data?: Partial<User>) { super(data); } }
Now bind this model to USER_MODEL key in application.ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
Create CRUD repository for the above model. Use loopback CLI.
lb4 repository
Add the verifier function for the strategy. You need to create a provider for the same. You can add your application specific business logic for client auth here. Here is simple example for JWT tokens.
import {Provider} from '@loopback/context'; import {repository} from '@loopback/repository'; import {verify} from 'jsonwebtoken'; import {VerifyFunction} from 'loopback4-authentication'; import {User} from '../models/user.model'; export class BearerTokenVerifyProvider implements Provider<VerifyFunction.BearerFn> { constructor( @repository(RevokedTokenRepository) public revokedTokenRepository: RevokedTokenRepository, ) {} value(): VerifyFunction.BearerFn { return async token => { if (token && (await this.revokedTokenRepository.get(token))) { throw new HttpErrors.Unauthorized('Token Revoked'); } const user = verify(token, process.env.JWT_SECRET as string, { issuer: process.env.JWT_ISSUER, }) as User; return user; }; } }
The above example has an import and injection of a RevokedTokenRepository, which could be used to keep track of revoked tokens in a datasource like Redis. You can find an implementation of this repository here and the Redis datasource here.
Please note the Verify function type VerifyFunction.BearerFn
Now bind this provider to the application in application.ts.
import {AuthenticationComponent, Strategies} from 'loopback4-authentication';
// Add authentication component this.component(AuthenticationComponent); // Customize authentication verify handlers this.bind(Strategies.Passport.BEARER_TOKEN_VERIFIER).toProvider( BearerTokenVerifyProvider, );
Finally, add the authenticate function as a sequence action to sequence.ts.
export class MySequence implements SequenceHandler { constructor( @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, @inject(SequenceActions.SEND) public send: Send, @inject(SequenceActions.REJECT) public reject: Reject, @inject(AuthenticationBindings.USER_AUTH_ACTION) protected authenticateRequest: AuthenticateFn<AuthUser>, ) {} async handle(context: RequestContext) { try { const {request, response} = context; const route = this.findRoute(request); const args = await this.parseParams(request, route); request.body = args[args.length - 1]; const authUser: AuthUser = await this.authenticateRequest(request); const result = await this.invoke(route, args); this.send(response, result); } catch (err) { this.reject(context, err); } } }
After this, you can use decorator to apply auth to controller functions wherever needed. See below.
@authenticate(STRATEGY.BEARER) @get('/users', { responses: { '200': { description: 'Array of User model instances', content: { 'application/json': { schema: {type: 'array', items: {'x-ts-type': User}}, }, }, }, }, }) async find( @param.query.object('filter', getFilterSchemaFor(User)) filter?: Filter, ): Promise<User[]> { return await this.userRepository.find(filter); }
For accessing the authenticated AuthUser model reference, you can inject the CURRENT_USER provider, provided by the extension, which is populated by the auth action sequence above.
@inject.getter(AuthenticationBindings.CURRENT_USER) private readonly getCurrentUser: Getter<User>,
In order to use it, run npm install passport-local.
First, create a AuthUser model implementing the IAuthUser interface. You can implement the interface in the user model itself. See sample below.
@model({ name: 'users', }) export class User extends Entity implements IAuthUser { @property({ type: 'number', id: true, }) id?: number; @property({ type: 'string', required: true, name: 'first_name', }) firstName: string; @property({ type: 'string', name: 'last_name', }) lastName: string; @property({ type: 'string', name: 'middle_name', }) middleName?: string; @property({ type: 'string', required: true, }) username: string; @property({ type: 'string', }) email?: string; @property({ type: 'string', }) password?: string; constructor(data?: Partial<User>) { super(data); } }
Now bind this model to USER_MODEL key in application.ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
Create CRUD repository for the above model. Use loopback CLI.
lb4 repository
Add the verifier function for the strategy. You need to create a provider for the same. You can add your application specific business logic for client auth here. Here is a simple example.
export class LocalPasswordVerifyProvider implements Provider<VerifyFunction.LocalPasswordFn> { constructor( @repository(UserRepository) public userRepository: UserRepository, ) {} value(): VerifyFunction.LocalPasswordFn { return async (username: any, password: any) => { try { const user: AuthUser = new AuthUser( await this.userRepository.verifyPassword(username, password), ); return user; } catch (error) { throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials) .message; } }; } }
Please note the Verify function type VerifyFunction.LocalPasswordFn
Now bind this provider to the application in application.ts.
import {AuthenticationComponent, Strategies} from 'loopback4-authentication';
// Add authentication component this.component(AuthenticationComponent); // Customize authentication verify handlers this.bind(Strategies.Passport.LOCAL_PASSWORD_VERIFIER).toProvider( LocalPasswordVerifyProvider, );
Finally, add the authenticate function as a sequence action to sequence.ts.
export class MySequence implements SequenceHandler { constructor( @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, @inject(SequenceActions.SEND) public send: Send, @inject(SequenceActions.REJECT) public reject: Reject, @inject(AuthenticationBindings.USER_AUTH_ACTION) protected authenticateRequest: AuthenticateFn<AuthUser>, ) {} async handle(context: RequestContext) { try { const {request, response} = context; const route = this.findRoute(request); const args = await this.parseParams(request, route); request.body = args[args.length - 1]; const authUser: AuthUser = await this.authenticateRequest(request); const result = await this.invoke(route, args); this.send(response, result); } catch (err) { this.reject(context, err); } } }
After this, you can use decorator to apply auth to controller functions wherever needed. See below.
@authenticate(STRATEGY.LOCAL) @post('/auth/login', { responses: { [STATUS_CODE.OK]: { description: 'Auth Code', content: { [CONTENT_TYPE.JSON]: Object, }, }, }, }) async login( @requestBody() req: LoginRequest, ): Promise<{ code: string; }> { ...... }
For accessing the authenticated AuthUser model reference, you can inject the CURRENT_USER provider, provided by the extension, which is populated by the auth action sequence above.
@inject.getter(AuthenticationBindings.CURRENT_USER) private readonly getCurrentUser: Getter<User>,
In order to use it, run npm install passport-oauth2-resource-owner-password.
First, create an AuthClient model implementing the IAuthClient interface. The purpose of this model is to store oauth registered clients for the app in the DB. See sample below.
@model({ name: 'auth_clients', }) export class AuthClient extends Entity implements IAuthClient { @property({ type: 'number', id: true, }) id?: number; @property({ type: 'string', required: true, name: 'client_id', }) clientId: string; @property({ type: 'string', required: true, name: 'client_secret', }) clientSecret: string; @property({ type: 'array', itemType: 'number', name: 'user_ids', }) userIds: number[]; constructor(data?: Partial<AuthClient>) { super(data); } }
Next, create a AuthUser model implementing the IAuthUser interface. You can implement the interface in the user model itself. See sample below.
@model({ name: 'users', }) export class User extends Entity implements IAuthUser { @property({ type: 'number', id: true, }) id?: number; @property({ type: 'string', required: true, name: 'first_name', }) firstName: string; @property({ type: 'string', name: 'last_name', }) lastName: string; @property({ type: 'string', name: 'middle_name', }) middleName?: string; @property({ type: 'string', required: true, }) username: string; @property({ type: 'string', }) email?: string; @property({ type: 'string', }) password?: string; constructor(data?: Partial<User>) { super(data); } }
Now bind this model to USER_MODEL key in application.ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
Create CRUD repository for both of the above models. Use loopback CLI.
lb4 repository
Add the verifier function for the strategy. You need to create a provider for the same. You can add your application specific business logic for client auth here. Here is a simple example.
export class ResourceOwnerVerifyProvider implements Provider<VerifyFunction.ResourceOwnerPasswordFn> { constructor( @repository(UserRepository) public userRepository: UserRepository, @repository(AuthClientRepository) public authClientRepository: AuthClientRepository, ) {} value(): VerifyFunction.ResourceOwnerPasswordFn { return async (clientId, clientSecret, username, password) => { const user = await this.userRepository.verifyPassword(username, password); if (!user) { throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); } const client = await this.authClientRepository.findOne({ where: { clientId, }, }); if (!client || client.userIds.indexOf(user.id || 0) < 0) { throw new HttpErrors.Unauthorized(AuthErrorKeys.ClientInvalid); } else if (!client.clientSecret || client.clientSecret !== clientSecret) { throw new HttpErrors.Unauthorized( AuthErrorKeys.ClientVerificationFailed, ); } return { client, user, }; }; } }
Please note the Verify function type VerifyFunction.LocalPasswordFn. Also, in this case, verifier will return AuthClient as well as User model.
Now bind this provider to the application in application.ts.
import {AuthenticationComponent, Strategies} from 'loopback4-authentication';
// Add authentication component this.component(AuthenticationComponent); // Customize authentication verify handlers this.bind(Strategies.Passport.RESOURCE_OWNER_PASSWORD_VERIFIER).toProvider( ResourceOwnerVerifyProvider, );
Finally, add the authenticate function as a sequence action to sequence.ts.
export class MySequence implements SequenceHandler { constructor( @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, @inject(SequenceActions.SEND) public send: Send, @inject(SequenceActions.REJECT) public reject: Reject, @inject(AuthenticationBindings.USER_AUTH_ACTION) protected authenticateRequest: AuthenticateFn<AuthUser>, ) {} async handle(context: RequestContext) { try { const {request, response} = context; const route = this.findRoute(request); const args = await this.parseParams(request, route); request.body = args[args.length - 1]; const authUser: AuthUser = await this.authenticateRequest(request); const result = await this.invoke(route, args); this.send(response, result); } catch (err) { this.reject(context, err); } } }
After this, you can use decorator to apply auth to controller functions wherever needed. See below.
@authenticate(STRATEGY.OAUTH2_RESOURCE_OWNER_GRANT) @post('/auth/login-token', { responses: { [STATUS_CODE.OK]: { description: 'Token Response Model', content: { [CONTENT_TYPE.JSON]: { schema: {'x-ts-type': TokenResponse}, }, }, }, }, }) async loginWithClientUser( @requestBody() req: LoginRequest, ): Promise<TokenResponse> { ...... }
For accessing the authenticated AuthUser and AuthClient model reference, you can inject the CURRENT_USER and CURRENT_CLIENT provider, provided by the extension, which is populated by the auth action sequence above.
@inject.getter(AuthenticationBindings.CURRENT_USER) private readonly getCurrentUser: Getter<User>, @inject.getter(AuthenticationBindings.CURRENT_CLIENT) private readonly getCurrentClient: Getter<AuthClient>,
First, create a OtpCache model. This model should have OTP and few details of user and client (which will be used to retrieve them from database), it will be used to verify otp and get user, client. See sample below.
@model() export class OtpCache extends Entity { @property({ type: 'string', }) otp: string; @property({ type: 'string', }) userId: string; @property({ type: 'string', }) clientId: string; @property({ type: 'string', }) clientSecret: string; constructor(data?: Partial<OtpCache>) { super(data); } }
Create redis-repository for the above model. Use loopback CLI.
lb4 repository
Here is a simple example.
import {OtpCache} from '../models'; import {AuthCacheSourceName} from 'loopback4-authentication'; export class OtpCacheRepository extends DefaultKeyValueRepository<OtpCache> { constructor( @inject(`datasources.${AuthCacheSourceName}`) dataSource: juggler.DataSource, ) { super(OtpCache, dataSource); } }
Add the verifier function for the strategy. You need to create a provider for the same. You can add your application specific business logic for auth here. Here is a simple example.
export class OtpVerifyProvider implements Provider<VerifyFunction.OtpAuthFn> { constructor( @repository(UserRepository) public userRepository: UserRepository, @repository(OtpCacheRepository) public otpCacheRepo: OtpCacheRepository, ) {} value(): VerifyFunction.OtpAuthFn { return async (key: string, otp: string) => { const otpCache = await this.otpCacheRepo.get(key); if (!otpCache) { throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); } if (otpCache.otp.toString() !== otp) { throw new HttpErrors.Unauthorized('Invalid OTP'); } return this.userRepository.findById(otpCache.userId); }; } }
Please note the Verify function type VerifyFunction.OtpAuthFn
Now bind this provider to the application in application.ts.
import {AuthenticationComponent, Strategies} from 'loopback4-authentication';
// Add authentication component this.component(AuthenticationComponent); // Customize authentication verify handlers this.bind(Strategies.Passport.OTP_VERIFIER).toProvider(OtpVerifyProvider);
Finally, add the authenticate function as a sequence action to sequence.ts.
export class MySequence implements SequenceHandler { constructor( @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, @inject(SequenceActions.SEND) public send: Send, @inject(SequenceActions.REJECT) public reject: Reject, @inject(AuthenticationBindings.USER_AUTH_ACTION) protected authenticateRequest: AuthenticateFn<AuthUser>, ) {} async handle(context: RequestContext) { try { const {request, response} = context; const route = this.findRoute(request); const args = await this.parseParams(request, route); request.body = args[args.length - 1]; const authUser: AuthUser = await this.authenticateRequest(request); const result = await this.invoke(route, args); this.send(response, result); } catch (err) { this.reject(context, err); } } }
Then, you need to create APIs, where you will first authenticate the user, and then send the OTP to user's email/phone. See below.
//You can use your other strategies also @authenticate(STRATEGY.LOCAL) @post('/auth/send-otp', { responses: { [STATUS_CODE.OK]: { description: 'Send Otp', content: { [CONTENT_TYPE.JSON]: Object, }, }, }, }) async login( @requestBody() req: LoginRequest, ): Promise<{ key: string; }> { // User is authenticated before this step. // Now follow these steps: // 1. Create a unique key. // 2. Generate and send OTP to user's email/phone. // 3. Store the details in redis-cache using key created in step-1. (Refer OtpCache model mentioned above) // 4. Response will be the key created in step-1 }
After this, create an API with @@authenticate(STRATEGY.OTP) decorator. See below.
@authenticate(STRATEGY.OTP) @post('/auth/login-otp', { responses: { [STATUS_CODE.OK]: { description: 'Auth Code', content: { [CONTENT_TYPE.JSON]: Object, }, }, }, }) async login( @requestBody() req: { key: 'string'; otp: 'string'; }, ): Promise<{ code: string; }> { ...... }
For accessing the authenticated AuthUser model reference, you can inject the CURRENT_USER provider, provided by the extension, which is populated by the auth action sequence above.
@inject.getter(AuthenticationBindings.CURRENT_USER) private readonly getCurrentUser: Getter<User>,
In order to use it, run npm install passport-google-oauth20 and npm install @types/passport-google-oauth20.
First, create a AuthUser model implementing the IAuthUser interface. You can implement the interface in the user model itself. See sample below.
@model({ name: 'users', }) export class User extends Entity implements IAuthUser { @property({ type: 'number', id: true, }) id?: number; @property({ type: 'string', required: true, name: 'first_name', }) firstName: string; @property({ type: 'string', name: 'last_name', }) lastName: string; @property({ type: 'string', name: 'middle_name', }) middleName?: string; @property({ type: 'string', required: true, }) username: string; @property({ type: 'string', }) email?: string; // Auth provider - 'google' @property({ type: 'string', required: true, name: 'auth_provider', }) authProvider: string; // Id from external provider @property({ type: 'string', name: 'auth_id', }) authId?: string; @property({ type: 'string', name: 'auth_token', }) authToken?: string; @property({ type: 'string', }) password?: string; constructor(data?: Partial<User>) { super(data); } }
Now bind this model to USER_MODEL key in application.ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
Create CRUD repository for the above model. Use loopback CLI.
lb4 repository
Add the verifier function for the strategy. You need to create a provider for the same. You can add your application specific business logic for client auth here. Here is a simple example.
import {Provider} from '@loopback/context'; import {repository} from '@loopback/repository'; import {HttpErrors} from '@loopback/rest'; import {AuthErrorKeys, VerifyFunction} from 'loopback4-authentication'; import {Tenant} from '../../../models'; import {UserCredentialsRepository, UserRepository} from '../../../repositories'; import {AuthUser} from '../models/auth-user.model'; export class GoogleOauth2VerifyProvider implements Provider<VerifyFunction.GoogleAuthFn> { constructor( @repository(UserRepository) public userRepository: UserRepository, @repository(UserCredentialsRepository) public userCredsRepository: UserCredentialsRepository, ) {} value(): VerifyFunction.GoogleAuthFn { return async (accessToken, refreshToken, profile) => { const user = await this.userRepository.findOne({ where: { /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ email: (profile as any)._json.email, }, }); if (!user) { throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); } if ( !user || user.authProvider !== 'google' || user.authId !== profile.id ) { throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); } const authUser: AuthUser = new AuthUser(user); authUser.permissions = []; authUser.externalAuthToken = accessToken; authUser.externalRefreshToken = refreshToken; authUser.tenant = new Tenant({id: user.defaultTenant}); return authUser; }; } }
Please note the Verify function type VerifyFunction.LocalPasswordFn
Now bind this provider to the application in application.ts.
import {AuthenticationComponent, Strategies} from 'loopback4-authentication';
// Add authentication component this.component(AuthenticationComponent); // Customize authentication verify handlers this.bind(Strategies.Passport.GOOGLE_OAUTH2_VERIFIER).toProvider( GoogleOauth2VerifyProvider, );
Now, bind this provider to the application in application.ts.
import {GoogleAuthStrategyFactoryProvider} from 'loopback4-authentication/passport-google-oauth2';
this.bind(Strategies.Passport.GOOGLE_OAUTH2_STRATEGY_FACTORY.key).toProvider( GoogleAuthStrategyFactoryProvider, );
Finally, add the authenticate function as a sequence action to sequence.ts.
export class MySequence implements SequenceHandler { constructor( @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, @inject(SequenceActions.SEND) public send: Send, @inject(SequenceActions.REJECT) public reject: Reject, @inject(AuthenticationBindings.USER_AUTH_ACTION) protected authenticateRequest: AuthenticateFn<AuthUser>, ) {} async handle(context: RequestContext) { try { const {request, response} = context; const route = this.findRoute(request); const args = await this.parseParams(request, route); request.body = args[args.length - 1]; const authUser: AuthUser = await this.authenticateRequest( request, response, ); const result = await this.invoke(route, args); this.send(response, result); } catch (err) { this.reject(context, err); } } }
After this, you can use decorator to apply auth to controller functions wherever needed. See below.
@authenticateClient(STRATEGY.CLIENT_PASSWORD) @authenticate( STRATEGY.GOOGLE_OAUTH2, { accessType: 'offline', scope: ['profile', 'email'], authorizationURL: process.env.GOOGLE_AUTH_URL, callbackURL: process.env.GOOGLE_AUTH_CALLBACK_URL, clientID: process.env.GOOGLE_AUTH_CLIENT_ID, clientSecret: process.env.GOOGLE_AUTH_CLIENT_SECRET, tokenURL: process.env.GOOGLE_AUTH_TOKEN_URL, }, (req: Request) => { return { accessType: 'offline', state: Object.keys(req.query) .map(key => key + '=' + req.query[key]) .join('&'), }; }, ) @authorize(['*']) @get('/auth/google', { responses: { [STATUS_CODE.OK]: { description: 'Token Response', content: { [CONTENT_TYPE.JSON]: { schema: {'x-ts-type': TokenResponse}, }, }, }, }, }) async loginViaGoogle( @param.query.string('client_id') clientId?: string, @param.query.string('client_secret') clientSecret?: string, ): Promise<void> {} @authenticate( STRATEGY.GOOGLE_OAUTH2, { accessType: 'offline', scope: ['profile', 'email'], authorizationURL: process.env.GOOGLE_AUTH_URL, callbackURL: process.env.GOOGLE_AUTH_CALLBACK_URL, clientID: process.env.GOOGLE_AUTH_CLIENT_ID, clientSecret: process.env.GOOGLE_AUTH_CLIENT_SECRET, tokenURL: process.env.GOOGLE_AUTH_TOKEN_URL, }, (req: Request) => { return { accessType: 'offline', state: Object.keys(req.query) .map(key => `${key}=${req.query[key]}`) .join('&'), }; }, ) @authorize(['*']) @get('/auth/google-auth-redirect', { responses: { [STATUS_CODE.OK]: { description: 'Token Response', content: { [CONTENT_TYPE.JSON]: { schema: {'x-ts-type': TokenResponse}, }, }, }, }, }) async googleCallback( @param.query.string('code') code: string, @param.query.string('state') state: string, @inject(RestBindings.Http.RESPONSE) response: Response, ): Promise<void> { const clientId = new URLSearchParams(state).get('client_id'); if (!clientId || !this.user) { throw new HttpErrors.Unauthorized(AuthErrorKeys.ClientInvalid); } const client = await this.authClientRepository.findOne({ where: { clientId: clientId, }, }); if (!client || !client.redirectUrl) { throw new HttpErrors.Unauthorized(AuthErrorKeys.ClientInvalid); } try { const codePayload: ClientAuthCode<User> = { clientId, user: this.user, }; const token = jwt.sign(codePayload, client.secret, { expiresIn: client.authCodeExpiration, audience: clientId, subject: this.user.username, issuer: process.env.JWT_ISSUER, }); response.redirect(`${client.redirectUrl}?code=${token}`); } catch (error) { throw new HttpErrors.InternalServerError(AuthErrorKeys.UnknownError); } }
Please note above that we are creating two new APIs for google auth. The first one is for UI clients to hit. We are authenticating client as well, then passing the details to the google auth. Then, the actual authentication is done by google authorization url, which redirects to the second API we created after success. The first API method body is empty as we do not need to handle its response. The google auth provider in this package will do the redirection for you automatically.
For accessing the authenticated AuthUser model reference, you can inject the CURRENT_USER provider, provided by the extension, which is populated by the auth action sequence above.
@inject.getter(AuthenticationBindings.CURRENT_USER) private readonly getCurrentUser: Getter<User>,
In order to use it, run npm install passport-instagram.
First, create a AuthUser model implementing the IAuthUser interface. You can implement the interface in the user model itself. See sample below.
@model({ name: 'users', }) export class User extends Entity implements IAuthUser { @property({ type: 'number', id: true, }) id?: number; @property({ type: 'string', required: true, name: 'first_name', }) firstName: string; @property({ type: 'string', name: 'last_name', }) lastName: string; @property({ type: 'string', name: 'middle_name', }) middleName?: string; @property({ type: 'string', required: true, }) username: string; @property({ type: 'string', }) email?: string; // Auth provider - 'instagram' @property({ type: 'string', required: true, name: 'auth_provider', }) authProvider: string; // Id from external provider @property({ type: 'string', name: 'auth_id', }) authId?: string; @property({ type: 'string', name: 'auth_token', }) authToken?: string; @property({ type: 'string', }) password?: string; constructor(data?: Partial<User>) { super(data); } }
Now bind this model to USER_MODEL key in application.ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
Create CRUD repository for the above model. Use loopback CLI.
lb4 repository
Add the verifier function for the strategy. You need to create a provider for the same. You can add your application specific business logic for client auth here. Here is a simple example.
import {Provider} from '@loopback/context'; import {repository} from '@loopback/repository'; import {HttpErrors} from '@loopback/rest'; import {AuthErrorKeys, VerifyFunction} from 'loopback4-authentication'; import {Tenant} from '../../../models'; import {UserCredentialsRepository, UserRepository} from '../../../repositories'; import {AuthUser} from '../models/auth-user.model'; export class InstagramOauth2VerifyProvider implements Provider<VerifyFunction.InstagramAuthFn> { constructor( @repository(UserRepository) public userRepository: UserRepository, @repository(UserCredentialsRepository) public userCredsRepository: UserCredentialsRepository, ) {} value(): VerifyFunction.InstagramAuthFn { return async (accessToken, refreshToken, profile) => { const user = await this.userRepository.findOne({ where: { /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ email: (profile as any)._json.email, }, }); if (!user) { throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); } if ( !user || user.authProvider !== 'instagram' || user.authId !== profile.id ) { throw new HttpErrors.Unauthorized
sourcefuse/loopback4-authentication is a public repo on GitHub by sourcefuse. A loopback-next extension for authentication feature. Oauth strategies supported.
It declares the MIT license on GitHub.
Its maker has not tagged a system, so it is unknown here. Its README says what it needs.
Open "Take it" on its page: Take My Repo writes an install plan for your machine and hands it to your AI agent (Claude Code, Codex or Cursor), which asks you before each step. Take My Repo has not tested it on any machine.
Its last change on GitHub was on 2026-11-19. GitHub counts 0 open issues and pull requests. Take My Repo does not judge from that whether it is maintained.
Nobody has reviewed it on Take My Repo yet, which says nothing about its quality.
The Single Sign-On Multi-Factor portal for web apps. OpenID Certified™ and Post-Quantum Cryptography Ready.
The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
Reverse engineer anything with agents, from app behavior down to native binaries.

A list of Free Software network services and web applications which can be hosted on your own servers


A Catalog of Microservices Created by SourceFuse for the Community

Loopback 4 starter application. Multi-tenant architecture supported. Authentication, Authorization, Soft deletes, environment vars, Audit logs, included.
An Authorization Extension for LoopBack 4 Applications
A loopback-next extension for implementing audit logs in loopback applications for all DB transactions.
This page is also available as structured data, with the same sources and dates.
/api/v1/resources/gh/sourcefuse/loopback4-authenticationTake My Repo has not tested this on your machine. Requirements come from the project's own documentation.