-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Connect session create & user create to SSO
- Loading branch information
1 parent
b0280fb
commit e99f9e6
Showing
25 changed files
with
859 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/backend/src/core/auth/services/sign_in/sign_in.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { getConfigFile } from '@/helpers/config'; | ||
import { EmailHelperService } from '@/helpers/email/email.service'; | ||
import { InternalDatabaseService } from '@/utils/database/internal_database.service'; | ||
import { | ||
ForbiddenException, | ||
HttpException, | ||
HttpStatus, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
import { Request, Response } from 'express'; | ||
import { SignInAuthBody, SignInAuthObj } from 'vitnode-shared/auth/auth.dto'; | ||
|
||
import { verifyPassword } from '../../helpers/password'; | ||
import { SendConfirmEmailAuthService } from '../sign_up/send.confirm_email.service'; | ||
import { HelperSignInAuthService } from './helper.service'; | ||
|
||
@Injectable() | ||
export class SignInAuthService { | ||
constructor( | ||
private readonly databaseService: InternalDatabaseService, | ||
private readonly signInHelper: HelperSignInAuthService, | ||
private readonly sendConfirmEmailCoreSessionsService: SendConfirmEmailAuthService, | ||
private readonly mailService: EmailHelperService, | ||
) {} | ||
|
||
async singIn({ | ||
req, | ||
res, | ||
body: { admin, email: emailRaw, password, ...rest }, | ||
}: { | ||
body: SignInAuthBody; | ||
req: Request; | ||
res: Response; | ||
}): Promise<SignInAuthObj> { | ||
const { settings } = getConfigFile(); | ||
const email = emailRaw.toLowerCase(); | ||
const user = await this.databaseService.db.query.core_users.findFirst({ | ||
where: (table, { eq }) => eq(table.email, email), | ||
with: { | ||
confirm_email: true, | ||
}, | ||
columns: { | ||
id: true, | ||
email_verified: true, | ||
group_id: true, | ||
name: true, | ||
password: true, | ||
language: true, | ||
name_seo: true, | ||
avatar_color: true, | ||
}, | ||
}); | ||
if (!user?.password) { | ||
throw new ForbiddenException('ACCESS_DENIED'); | ||
} | ||
|
||
const validPassword = await verifyPassword(password, user.password); | ||
if (!validPassword) { | ||
throw new ForbiddenException('ACCESS_DENIED'); | ||
} | ||
|
||
if ( | ||
!user.email_verified && | ||
settings.authorization.require_confirm_email && | ||
this.mailService.checkIfEnable() | ||
) { | ||
await this.sendConfirmEmailCoreSessionsService.sendConfirmEmail({ | ||
userId: user.id, | ||
}); | ||
|
||
throw new HttpException('EMAIL_NOT_VERIFIED', HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
// If admin mode is enabled, check if user has access to admin cp | ||
if (admin) { | ||
const accessToAdminCP = | ||
await this.databaseService.db.query.core_admin_permissions.findFirst({ | ||
where: (table, { eq, or }) => | ||
or( | ||
user.group_id ? eq(table.group_id, user.group_id) : undefined, | ||
eq(table.user_id, user.id), | ||
), | ||
}); | ||
if (!accessToAdminCP) { | ||
throw new ForbiddenException('ACCESS_DENIED'); | ||
} | ||
} | ||
|
||
const loginToken = await this.signInHelper.createSession({ | ||
req, | ||
res, | ||
body: { admin, email, user_id: user.id, name: user.name, ...rest }, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { password: _, ...userWithoutPassword } = user; | ||
|
||
return { | ||
login_token: loginToken, | ||
...userWithoutPassword, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.