passport-local#Strategy TypeScript Examples
The following examples show how to use
passport-local#Strategy.
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: local.strategy.ts From nestjs-rest-sample with GNU General Public License v3.0 | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'username',
passwordField: 'password',
});
}
// When using Observable as return type, the exeption in the pipeline is ignored.
// In our case, the `UnauthorizedException` is **NOT** caught and handled as expected.
// The flow is NOT prevented by the exception and continue to send a `Observable` to
// the next step aka calling `this.authService.login` in `AppController#login` method.
// Then the jwt token is generated in any case(eg. wrong username or wrong password),
// the authenticatoin worflow does not work as expected.
//
// The solution is customizing `PassportSerializer`.
// Example: https://github.com/jmcdo29/zeldaPlay/blob/master/apps/api/src/app/auth/session.serializer.ts
//
// validate(username: string, password: string): Observable<any> {
// return this.authService
// .validateUser(username, password)
// .pipe(throwIfEmpty(() => new UnauthorizedException()));
// }
async validate(username: string, password: string): Promise<UserPrincipal> {
const user: UserPrincipal = await lastValueFrom(
this.authService.validateUser(username, password),
);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Example #2
Source File: local.strategy.ts From knests with MIT License | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
usernameField: 'email',
passwordField: 'password',
});
}
async validate(credentials: LoginDTO): Promise<any> {
const user = await this.authService.validateUser(credentials);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Example #3
Source File: local.strategy.ts From nestjs-starter with MIT License | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
usernameField: 'username',
passwordField: 'password',
});
}
async validate(username: string, password: string): Promise<any> {
const user = await this.authService.validateUser(username, password);
if (!user) {
throw new UnauthorizedException('Login user or password does not match.');
}
return user;
}
}
Example #4
Source File: HttpLocalStrategy.ts From typescript-clean-architecture with MIT License | 6 votes |
@Injectable()
export class HttpLocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: HttpAuthService) {
super({
usernameField: ApiServerConfig.LOGIN_USERNAME_FIELD,
passwordField: ApiServerConfig.LOGIN_PASSWORD_FIELD,
});
}
public async validate(username: string, password: string): Promise<HttpUserPayload> {
const user: HttpUserPayload = CoreAssert.notEmpty(
await this.authService.validateUser(username, password),
Exception.new({code: Code.WRONG_CREDENTIALS_ERROR})
);
return user;
}
}
Example #5
Source File: local.strategy.ts From MyAPI with MIT License | 6 votes |
/**
* Class local strategy
*
* @export
* @class LocalStrategy
* @extends {PassportStrategy(Strategy)}
*/
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
constructor(private readonly authService: AuthService) {
super({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: false
})
}
/**
* Validate account
*
* @param {String} id
* @param {String} password
* @memberof LocalStrategy
*/
async validate(id: string, password: string): Promise<User> {
const user = await this.authService.validateUser(id, password)
if (!user) {
throw new UnauthorizedException()
}
if(user.status == UserStatus.Inactive){
throw new UnauthorizedException(ERRORS.USER_INACTIVE)
}
return user
}
}
Example #6
Source File: local.strategy.ts From api with GNU Affero General Public License v3.0 | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}
async validate(username: string, password: string): Promise<any> {
const user = await this.authService.validateUser(username, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Example #7
Source File: local.strategy.ts From nestjs-starter-rest-api with MIT License | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy, STRATEGY_LOCAL) {
constructor(
private authService: AuthService,
private readonly logger: AppLogger,
) {
// Add option passReqToCallback: true to configure strategy to be request-scoped.
super({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true,
});
this.logger.setContext(LocalStrategy.name);
}
async validate(
request: Request,
username: string,
password: string,
): Promise<UserAccessTokenClaims> {
const ctx = createRequestContext(request);
this.logger.log(ctx, `${this.validate.name} was called`);
const user = await this.authService.validateUser(ctx, username, password);
// Passport automatically creates a user object, based on the value we return from the validate() method,
// and assigns it to the Request object as req.user
return user;
}
}
Example #8
Source File: local.ts From Deep-Lynx with MIT License | 6 votes |
// just a wrapper for the passport.js local authentication method
export function SetLocalAuthMethod(app: express.Application) {
passport.use(
new Strategy({passReqToCallback: true}, (req, username, password, done) => {
void UserMapper.Instance.RetrieveByEmail(username).then((result) => {
if (result.isError) return done(result.error);
if (!result.value) return done('unable to login as provided user');
bcrypt
.compare(password, result.value.password!)
.then((match) => {
if (match) return done(null, result.value);
return done(null, false);
})
.catch((e) => {
Logger.error(`error comparing hashed passwords ${e}`);
return done(null, false);
});
});
}),
);
}
Example #9
Source File: local.strategy.ts From pandaid with MIT License | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({ usernameField: 'email' })
}
validate(email: string, password: string) {
const user = this.authService.validateUser({ email, password })
if (!user) {
throw new UnauthorizedException()
}
return user
}
}
Example #10
Source File: local.strategy.ts From NestJs-youtube with MIT License | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
usernameField: 'email',
passwordField: 'password',
});
}
async validate(email: string, password: string): Promise<any> {
const user = await this.authService.validateUser(email, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Example #11
Source File: local.strategy.ts From nest-js-quiz-manager with MIT License | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}
async validate(email: string, password: string) {
const user = await this.authService.validateUserCreds(email, password);
if (!user) throw new UnauthorizedException();
return user;
}
}
Example #12
Source File: local.stratergy.ts From uniauth-backend with MIT License | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}
async validate(token: string): Promise<any> {
const participant = await this.authService.validateUser(token);
if (!participant) {
throw new UnauthorizedException();
}
return participant;
}
}
Example #13
Source File: local.strategy.ts From barista with Apache License 2.0 | 6 votes |
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private readonly userService: UserService, private readonly ldapService: LdapService) {
super();
}
async validate(username: string, password: string): Promise<any> {
let user: any = null;
if (process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase() === 'test') {
// If this is a test, let's go ahead and validate
// TODO: Add a provision to allow for testing of negative login conditions
user = {
displayName: '',
id: 0,
role: 'test',
userName: 'TestUser',
groups: ['group1', 'group1', 'group3'],
};
} else if (process.env.AUTH_TYPE === 'ldap') {
user = await this.ldapService.validateUser(username, password, false);
if (!user) {
user = await this.ldapService.validateUser(username, password, true);
}
if (user) {
user.groups = await this.ldapService.getUserGroups(username, password);
}
} else {
user = await this.userService.validateUser(username, password);
}
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Example #14
Source File: local.strategy.ts From nest-js-boilerplate with MIT License | 6 votes |
@Injectable()
export default class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'email',
passwordField: 'password',
});
}
public async validate(
email: string,
password: string,
): Promise<ValidateUserOutput> {
const user = await this.authService.validateUser(email, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Example #15
Source File: local.strategy.ts From nest-js-boilerplate with MIT License | 6 votes |
@Injectable()
export default class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true,
});
}
async validate(req: ExpressRequest, email: string, password: string): Promise<ValidateUserOutput> {
const errors = await validate(new SignInDto(req.body));
if (errors.length > 0) {
throw new BadRequestException(errors);
}
const user = await this.authService.validateUser(email, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Example #16
Source File: local.strategy.ts From nest-js-boilerplate with MIT License | 6 votes |
@Injectable()
export default class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'email',
passwordField: 'password',
});
}
async validate(
email: string,
password: string,
): Promise<ValidateUserOutput> {
const user = await this.authService.validateUser(email, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
Example #17
Source File: local.strategy.ts From nest-js-boilerplate with MIT License | 6 votes |
@Injectable()
export default class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true,
});
}
async validate(req: ExpressRequest, email: string, password: string): Promise<ValidateUserOutput> {
const errors = await validate(new SignInDto(req.body)) as ValidationError[];
if (errors.length > 0) {
throw new ValidationExceptions(errors);
}
const user = await this.authService.validateUser(email, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}