Update error handling and refactor

This commit is contained in:
Salman
2024-02-10 23:00:16 +05:30
parent 5cf1ec2400
commit da377f6fda
4 changed files with 13 additions and 19 deletions
+3 -12
View File
@@ -18,7 +18,6 @@ import { BadRequestError } from "@app/lib/errors";
import { logger } from "@app/lib/logger"; import { logger } from "@app/lib/logger";
import { fetchGithubEmails } from "@app/lib/requests/github"; import { fetchGithubEmails } from "@app/lib/requests/github";
import { AuthMethod } from "@app/services/auth/auth-type"; import { AuthMethod } from "@app/services/auth/auth-type";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
export const registerSsoRouter = async (server: FastifyZodProvider) => { export const registerSsoRouter = async (server: FastifyZodProvider) => {
const appCfg = getConfig(); const appCfg = getConfig();
@@ -42,7 +41,6 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
async (req, _accessToken, _refreshToken, profile, cb) => { async (req, _accessToken, _refreshToken, profile, cb) => {
try { try {
const email = profile?.emails?.[0]?.value; const email = profile?.emails?.[0]?.value;
const serverCfg = await getServerCfg();
if (!email) if (!email)
throw new BadRequestError({ throw new BadRequestError({
message: "Email not found", message: "Email not found",
@@ -54,8 +52,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
firstName: profile?.name?.givenName || "", firstName: profile?.name?.givenName || "",
lastName: profile?.name?.familyName || "", lastName: profile?.name?.familyName || "",
authMethod: AuthMethod.GOOGLE, authMethod: AuthMethod.GOOGLE,
callbackPort: req.query.state as string, callbackPort: req.query.state as string
serverCfg
}); });
cb(null, { isUserCompleted, providerAuthToken }); cb(null, { isUserCompleted, providerAuthToken });
} catch (error) { } catch (error) {
@@ -84,15 +81,12 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
try { try {
const ghEmails = await fetchGithubEmails(accessToken); const ghEmails = await fetchGithubEmails(accessToken);
const { email } = ghEmails.filter((gitHubEmail) => gitHubEmail.primary)[0]; const { email } = ghEmails.filter((gitHubEmail) => gitHubEmail.primary)[0];
const serverCfg = await getServerCfg();
const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({ const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({
email, email,
firstName: profile.displayName, firstName: profile.displayName,
lastName: "", lastName: "",
authMethod: AuthMethod.GITHUB, authMethod: AuthMethod.GITHUB,
callbackPort: req.query.state as string, callbackPort: req.query.state as string
serverCfg
// isSignupAllowed: Boolean(serverCfg.allowSignUp)
}); });
return cb(null, { isUserCompleted, providerAuthToken }); return cb(null, { isUserCompleted, providerAuthToken });
} catch (error) { } catch (error) {
@@ -121,15 +115,12 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
async (req: any, _accessToken: string, _refreshToken: string, profile: any, cb: any) => { async (req: any, _accessToken: string, _refreshToken: string, profile: any, cb: any) => {
try { try {
const email = profile.emails[0].value; const email = profile.emails[0].value;
const serverCfg = await getServerCfg();
const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({ const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({
email, email,
firstName: profile.displayName, firstName: profile.displayName,
lastName: "", lastName: "",
authMethod: AuthMethod.GITLAB, authMethod: AuthMethod.GITLAB,
callbackPort: req.query.state as string, callbackPort: req.query.state as string
serverCfg
// isSignupAllowed: Boolean(serverCfg.allowSignUp)
}); });
return cb(null, { isUserCompleted, providerAuthToken }); return cb(null, { isUserCompleted, providerAuthToken });
@@ -2,6 +2,7 @@ import { z } from "zod";
import { UsersSchema } from "@app/db/schemas"; import { UsersSchema } from "@app/db/schemas";
import { getConfig } from "@app/lib/config/env"; import { getConfig } from "@app/lib/config/env";
import { BadRequestError } from "@app/lib/errors";
import { authRateLimit } from "@app/server/config/rateLimiter"; import { authRateLimit } from "@app/server/config/rateLimiter";
import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types"; import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
@@ -29,7 +30,10 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
if (config?.allowSpecificDomainSignUp) { if (config?.allowSpecificDomainSignUp) {
const domain = email.split("@")[1]; const domain = email.split("@")[1];
if (domain !== config.allowSpecificDomainSignUp) throw new Error(`Unsupported email domain (${domain}).`); if (domain !== config.allowSpecificDomainSignUp)
throw new BadRequestError({
message: `User email domain (@${domain}) is not supported`
});
} }
await server.services.signup.beginEmailSignupProcess(email); await server.services.signup.beginEmailSignupProcess(email);
return { message: `Sent an email verification code to ${email}` }; return { message: `Sent an email verification code to ${email}` };
@@ -4,6 +4,7 @@ import { TUsers, UserDeviceSchema } from "@app/db/schemas";
import { getConfig } from "@app/lib/config/env"; import { getConfig } from "@app/lib/config/env";
import { generateSrpServerKey, srpCheckClientProof } from "@app/lib/crypto"; import { generateSrpServerKey, srpCheckClientProof } from "@app/lib/crypto";
import { BadRequestError } from "@app/lib/errors"; import { BadRequestError } from "@app/lib/errors";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
import { TAuthTokenServiceFactory } from "../auth-token/auth-token-service"; import { TAuthTokenServiceFactory } from "../auth-token/auth-token-service";
import { TokenType } from "../auth-token/auth-token-types"; import { TokenType } from "../auth-token/auth-token-types";
@@ -261,8 +262,10 @@ export const authLoginServiceFactory = ({ userDAL, tokenService, smtpService }:
/* /*
* OAuth2 login for google,github, and other oauth2 provider * OAuth2 login for google,github, and other oauth2 provider
* */ * */
const oauth2Login = async ({ email, firstName, lastName, authMethod, callbackPort, serverCfg }: TOauthLoginDTO) => { const oauth2Login = async ({ email, firstName, lastName, authMethod, callbackPort }: TOauthLoginDTO) => {
let user = await userDAL.findUserByEmail(email); let user = await userDAL.findUserByEmail(email);
const serverCfg = await getServerCfg();
const appCfg = getConfig(); const appCfg = getConfig();
if (!user) { if (!user) {
@@ -275,7 +278,7 @@ export const authLoginServiceFactory = ({ userDAL, tokenService, smtpService }:
if (domain !== serverCfg.allowSpecificDomainSignUp) if (domain !== serverCfg.allowSpecificDomainSignUp)
throw new BadRequestError({ throw new BadRequestError({
message: `User email domain (${domain}) is not supported`, message: `User email domain (@${domain}) is not supported`,
name: "Oauth 2 login" name: "Oauth 2 login"
}); });
} }
@@ -1,5 +1,3 @@
import { TSuperAdmin } from "@app/db/schemas/super-admin";
import { AuthMethod } from "./auth-type"; import { AuthMethod } from "./auth-type";
export type TLoginGenServerPublicKeyDTO = { export type TLoginGenServerPublicKeyDTO = {
@@ -30,6 +28,4 @@ export type TOauthLoginDTO = {
lastName?: string; lastName?: string;
authMethod: AuthMethod; authMethod: AuthMethod;
callbackPort?: string; callbackPort?: string;
isSignupAllowed?: boolean;
serverCfg?: TSuperAdmin;
}; };