Skip to content

Commit

Permalink
Merge pull request #78 from ansopedia/feat/redirect-on-google-sign-in
Browse files Browse the repository at this point in the history
Enhance Google Sign-In API to Support Redirect After Login
  • Loading branch information
sanjaysah101 authored Oct 22, 2024
2 parents 4a10ee6 + 43eb328 commit c24ae27
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/api/v1/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextFunction, Request, Response } from "express";

import { STATUS_CODES, envConstants } from "@/constants";
import { GoogleUser } from "@/types/passport-google";
import { sendResponse } from "@/utils";
import { isValidRedirectUrl, sendResponse } from "@/utils";

import { success } from "./auth.constant";
import { AuthService } from "./auth.service";
Expand Down Expand Up @@ -58,14 +58,24 @@ export class AuthController {

// TODO: used action token instead of access token
res.cookie("access-token", accessToken, {
httpOnly: false,
httpOnly: true,
secure: true,
sameSite: "strict",
maxAge: 60000, // 1 minute
});

// Instead of sending a JSON response, redirect to the client's URL
res.redirect(`${envConstants.CLIENT_URL}/login?success=true`);
// Validate and sanitize the redirect URL
const state = req.query.state as string;
let redirectUrl = `${envConstants.CLIENT_URL}/profile?success=true`; // Default redirect URL

if (state) {
const decodedUrl = Buffer.from(state, "base64").toString("utf-8");
if (isValidRedirectUrl(decodedUrl)) {
redirectUrl = decodedUrl;
}
}

res.redirect(redirectUrl);
} catch (error) {
next(error);
}
Expand Down
2 changes: 2 additions & 0 deletions src/middlewares/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ export const signInWithGoogleCallback = (req: Request, res: Response, next: Next
passport.authenticate("google", {
session: false,
failureRedirect: `${envConstants.CLIENT_URL}/login?error=failed`,
// Pass the state parameter to the callback
state: req.query.state as string | undefined,
})(req, res, next);
};
4 changes: 0 additions & 4 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@ declare global {
interface Request {
user: CreateUser;
}

interface User extends GoogleUser {
id: string;
}
}
}
File renamed without changes.
11 changes: 11 additions & 0 deletions src/utils/helper.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Request } from "express";

import { envConstants } from "@/constants";

export const getServerURL = (req: Request) => {
return `${req.protocol}://${req.get("host")}`;
};
Expand All @@ -8,3 +10,12 @@ export const generateRandomUsername = (): string => {
const randomString = Math.random().toString(36).substring(2, 10);
return `user_${randomString}`;
};

export const isValidRedirectUrl = (url: string): boolean => {
try {
const parsedUrl = new URL(url);
return parsedUrl.origin === envConstants.CLIENT_URL;
} catch {
return false;
}
};

0 comments on commit c24ae27

Please sign in to comment.