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

feat(auth): rewrite the implementation workflow #22

Merged
merged 9 commits into from
Jun 11, 2022
Prev Previous commit
Next Next commit
feat(app): log out the user
  • Loading branch information
tericcabrel committed Jun 9, 2022
commit b37333035b26f35abfdc8745a91e86828c624b63
2 changes: 1 addition & 1 deletion apps/core/src/resources/users/mutations/logout-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MutationResolvers } from '../../../types/graphql';
export const logoutUser: MutationResolvers['logoutUser'] = async (_parent, _args, context) => {
const userId = getAuthenticatedUser(context);

await context.db.session.delete(userId);
await context.db.session.deleteUserSessions(userId);

return true;
};
4 changes: 2 additions & 2 deletions packages/database/src/repositories/interfaces/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Session from '../../entities/session';
import BaseRepository from './_base';

type SessionRepositoryInterface = {
findByToken: (token: string) => Promise<Session | null>;
} & Pick<BaseRepository<Session>, 'create' | 'delete'>;
deleteUserSessions: (userId: string) => Promise<void>;
} & Pick<BaseRepository<Session>, 'create'>;

export default SessionRepositoryInterface;
12 changes: 2 additions & 10 deletions packages/database/src/repositories/session.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ export default class SessionRepository implements SessionRepositoryInterface {
});
}

async delete(id: string): Promise<void> {
const session = await prisma.session.findFirst({ where: { id } });

if (session) {
await prisma.session.delete({ where: { id } });
}
}

findByToken(token: string): Promise<Session | null> {
return prisma.session.findUnique({ where: { token } });
async deleteUserSessions(userId: string): Promise<void> {
await prisma.session.deleteMany({ where: { userId } });
}
}
8 changes: 2 additions & 6 deletions packages/domain/src/sessions/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ export default class SessionService {
return this._sessionRepository.create(createSessionDto.toSession());
}

async findByToken(token: string): Promise<Session | null> {
return this._sessionRepository.findByToken(token);
}

async delete(id: string): Promise<void> {
await this._sessionRepository.delete(id);
async deleteUserSessions(userId: string): Promise<void> {
await this._sessionRepository.deleteUserSessions(userId);
}
}