UNPKG

@raddiamond/nexauth-core

Version:

Core authentication plugin supporting Local, AD authentication

47 lines (46 loc) 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NexAuthStrategy = void 0; const passport_strategy_1 = require("passport-strategy"); const AuthService_1 = require("./AuthService"); class NexAuthStrategy extends passport_strategy_1.Strategy { constructor(options, verify) { super(); this.options = options; this.verify = verify; this.name = 'nexauth'; } async authenticate(req, _options) { const identifier = req.body?.[this.options.field]; const password = req.body?.password; // Optionally support OTP and other steps const otp = req.body?.otp; const securityQuestion = req.body?.securityQuestion; if (!identifier || !password) { return this.fail({ message: 'Missing credentials' }, 400); } try { const authService = new AuthService_1.AuthService(this.options.providerOptions); const input = { username: identifier, password, otp, securityQuestion }; const result = await authService.handleAuth(input); if (!result || !result.success || !result.user) { // If nextStep is present, inform the client what is needed next if (result && result.nextStep) { return this.fail({ message: 'Additional step required', nextStep: result.nextStep, error: result.error }, 401); } return this.fail({ message: result?.error || 'Invalid credentials' }, 401); } this.verify(result.user, (err, user) => { if (err) return this.error(err); if (!user) return this.fail({ message: 'Authentication failed' }, 401); this.success(user); }); } catch (err) { return this.error(err instanceof Error ? err : new Error(String(err))); } } } exports.NexAuthStrategy = NexAuthStrategy;