Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unused login function, auth token change #398

Merged
merged 2 commits into from
May 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 3 additions & 27 deletions services/UserAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { InjectManager } from 'typeorm-typedi-extensions';
import { EntityManager } from 'typeorm';
import { ExpressCheckinModel } from 'models/ExpressCheckinModel';
import { UserRepository } from '../repositories/UserRepository';
import { Uuid, ActivityType, UserState, UserRegistration } from '../types';
import { Uuid, ActivityType, UserState, UserRegistration, UserAccessType } from '../types';
import { Config } from '../config';
import { UserModel } from '../models/UserModel';
import Repositories, { TransactionsManager } from '../repositories';
import UserAccountService from './UserAccountService';

interface AuthToken {
uuid: Uuid;
admin: boolean;
accessType: UserAccessType;
}

@Service()
Expand Down Expand Up @@ -111,30 +111,6 @@ export default class UserAuthService {
return user;
}

public async login(email: string, pass: string): Promise<string> {
const authenticatedUser = await this.transactions.readWrite(async (txn) => {
let user = await Repositories
.user(txn)
.findByEmail(email.toLowerCase());
if (!user) throw new NotFoundError('There is no account associated with that email');
if (user.isBlocked()) throw new ForbiddenError('Your account has been blocked');
if (!(await user.verifyPass(pass))) throw new ForbiddenError('Incorrect password');
await Repositories.activity(txn).logActivity({
user,
type: ActivityType.ACCOUNT_LOGIN,
});
if (user.state === UserState.PASSWORD_RESET) {
user = await Repositories.user(txn).upsertUser(user, { state: UserState.ACTIVE });
}
return user;
});
const token: AuthToken = {
uuid: authenticatedUser.uuid,
admin: authenticatedUser.isAdmin(),
};
return jwt.sign(token, Config.auth.secret, { expiresIn: Config.auth.tokenLifespan });
}

public async checkCredentials(email: string, pass: string): Promise<UserModel> {
const authenticatedUser = await this.transactions.readWrite(async (txn) => {
const user = await Repositories
Expand All @@ -156,7 +132,7 @@ export default class UserAuthService {
public static generateAuthToken(user: UserModel): string {
const token: AuthToken = {
uuid: user.uuid,
admin: user.isAdmin(),
accessType: user.accessType,
};
return jwt.sign(token, Config.auth.secret, { expiresIn: Config.auth.tokenLifespan });
}
Expand Down
Loading