Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: upsert logic added to login service to upsert the authtoken.
  • Loading branch information
hanoak20 committed Jan 19, 2024
commit 467d513891965ff20a30286ff2d21d5532b895db
2 changes: 1 addition & 1 deletion src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface UserProfile {
};
}

export interface MigrationPayload {
export interface AppTokenPayload {
region: string;
user_id: string;
}
Expand Down
19 changes: 12 additions & 7 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Request } from "express";
import { config } from "../config";
import { safePromise } from "../utils/index";
import https from "../utils/https.utils";
import { LoginServiceType, MigrationPayload } from "../models/types";
import { LoginServiceType, AppTokenPayload } from "../models/types";
import { constants } from "../constants";
import { generateToken } from "../utils/jwt.utils";
import {
Expand Down Expand Up @@ -50,19 +50,24 @@ const login = async (req: Request): Promise<LoginServiceType> => {
if (!res?.data?.user)
throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER);

const migration_payload: MigrationPayload = {
const appTokenPayload: AppTokenPayload = {
region: userData?.region,
user_id: res?.data?.user.uid,
};

// Saving auth info in the DB
await AuthenticationModel.create({
...migration_payload,
authtoken: res?.data.user?.authtoken,
});
await AuthenticationModel.findOneAndUpdate(
appTokenPayload,
{
authtoken: res?.data.user?.authtoken,
},
{
upsert: true,
}
);

// JWT token generation
const app_token = generateToken(migration_payload);
const app_token = generateToken(appTokenPayload);

return {
data: {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/jwt.utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// src/utils/jwt.utils.ts
import jwt from "jsonwebtoken";
import { MigrationPayload } from "../models/types";
import { AppTokenPayload } from "../models/types";
import { config } from "../config";

// @typescript-eslint/no-explicit-any
export const generateToken = (payload: MigrationPayload): string => {
export const generateToken = (payload: AppTokenPayload): string => {
return jwt.sign(payload, config.APP_TOKEN_KEY, {
expiresIn: config.APP_TOKEN_EXP,
});
Expand Down