🚩 create user session when provision
create a user session after provisioning authentication if the account has been created previously.
This commit is contained in:
@ -329,6 +329,7 @@ model UserSession {
|
|||||||
deviceType String @db.VarChar(255)
|
deviceType String @db.VarChar(255)
|
||||||
deviceOs String @db.VarChar(255)
|
deviceOs String @db.VarChar(255)
|
||||||
deviceIp String @db.VarChar(255)
|
deviceIp String @db.VarChar(255)
|
||||||
|
browser String @db.VarChar(255)
|
||||||
isOnline Boolean @default(false)
|
isOnline Boolean @default(false)
|
||||||
lastOnline DateTime @default(now())
|
lastOnline DateTime @default(now())
|
||||||
validUntil DateTime
|
validUntil DateTime
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
|
import { User } from "@prisma/client";
|
||||||
import { UserHeaderInformation } from "../../../../helpers/http/userHeader/getUserHeaderInformation/types";
|
import { UserHeaderInformation } from "../../../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||||
import { findUserService } from "../../../user/services/internal/findUser.service";
|
import { findUserService } from "../../../user/services/internal/findUser.service";
|
||||||
|
import { createUserSessionService } from "../../../userSession/services/createUserSession.service";
|
||||||
|
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||||
|
|
||||||
export const OAuthUserProvisionService = async (
|
export const OAuthUserProvisionService = async (
|
||||||
payload: {
|
payload: {
|
||||||
@ -20,16 +23,20 @@ export const OAuthUserProvisionService = async (
|
|||||||
*
|
*
|
||||||
* This is just example!!
|
* This is just example!!
|
||||||
*/
|
*/
|
||||||
const providerId = `${payload.providerName}_${payload.openId}`;
|
try {
|
||||||
const findUserResult = await findUserService({
|
const providerId = `${payload.providerName}_${payload.openId}`;
|
||||||
identifier: providerId,
|
const findUserResult = (await findUserService({
|
||||||
queryTarget: "providerId",
|
identifier: providerId,
|
||||||
options: { verbosity: "exist" },
|
queryTarget: "providerId",
|
||||||
});
|
options: { verbosity: "full" },
|
||||||
|
})) as User;
|
||||||
|
|
||||||
if (findUserResult) {
|
if (findUserResult) {
|
||||||
return "Already Created";
|
return await createUserSessionService(findUserResult.id, userHeaderInfo);
|
||||||
} else {
|
} else {
|
||||||
return "Not Found";
|
return "Not Found";
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -27,10 +27,9 @@ export const findUserService = async (payload: getUserDataService) => {
|
|||||||
payload.options.verbosity
|
payload.options.verbosity
|
||||||
);
|
);
|
||||||
|
|
||||||
// Retrieving user data using the associated repository, if user not found return 404 response
|
// Retrieving user data using the associated repository, if user not found return 'false' response
|
||||||
const userData = await repoFn(payload.identifier, payload.options.include);
|
const userData = await repoFn(payload.identifier, payload.options.include);
|
||||||
if (!userData && existsVerbosity) return false;
|
if (!userData) return false;
|
||||||
if (!userData) throw new AppError(404, "User not found");
|
|
||||||
|
|
||||||
// If verbosity in 'exists' level and user is valid then just return 'true' value
|
// If verbosity in 'exists' level and user is valid then just return 'true' value
|
||||||
if (existsVerbosity) return true;
|
if (existsVerbosity) return true;
|
||||||
|
|||||||
@ -0,0 +1,15 @@
|
|||||||
|
import { Prisma } from "@prisma/client";
|
||||||
|
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||||
|
import { userSessionModel } from "../userSession.model";
|
||||||
|
|
||||||
|
export const createUserSessionRepository = async (
|
||||||
|
data: Prisma.UserSessionUncheckedCreateInput
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
return await userSessionModel.create({
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
import { Prisma } from "@prisma/client";
|
||||||
|
import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||||
|
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||||
|
import { createUserSessionRepository } from "../repositories/createUserSession.repository";
|
||||||
|
|
||||||
|
export const createUserSessionService = async (
|
||||||
|
userId: string,
|
||||||
|
userHeaderInfo: UserHeaderInformation
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const generateTokenExpirationDate =
|
||||||
|
Date.now() + Number(process.env.SESSION_EXPIRE!) * 1000;
|
||||||
|
|
||||||
|
const constructData = {
|
||||||
|
userId,
|
||||||
|
isAuthenticated: true,
|
||||||
|
deviceType: userHeaderInfo.deviceType,
|
||||||
|
deviceOs: userHeaderInfo.deviceOS,
|
||||||
|
deviceIp: userHeaderInfo.ip,
|
||||||
|
browser: userHeaderInfo.browser,
|
||||||
|
validUntil: new Date(generateTokenExpirationDate),
|
||||||
|
} as Prisma.UserSessionUncheckedCreateInput;
|
||||||
|
|
||||||
|
return createUserSessionRepository(constructData);
|
||||||
|
} catch (error) {
|
||||||
|
ErrorForwarder(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
3
src/modules/userSession/userSession.model.ts
Normal file
3
src/modules/userSession/userSession.model.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { prisma } from "../../utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const userSessionModel = prisma.userSession;
|
||||||
Reference in New Issue
Block a user