-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Add OAuth redirection and polished authentication (#212)
- Loading branch information
Showing
11 changed files
with
169 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,9 @@ FROM_EMAIL="your-name <[email protected]>" | |
|
||
JWT_SECRET=secret | ||
|
||
WEB_FRONTEND_URL=https://keyshade.xyz | ||
WORKSPACE_FRONTEND_URL=https://app.keyshade.xyz | ||
WEB_FRONTEND_URL=http://localhost:3000 | ||
PLATFORM_FRONTEND_URL=http://localhost:3100 | ||
PLATFORM_OAUTH_SUCCESS_REDIRECT_PATH=/oauth/signin | ||
PLATFORM_OAUTH_FAILURE_REDIRECT_PATH=/oauth/failure | ||
|
||
DOMAIN=localhost |
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
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,28 @@ | ||
import { User } from '@prisma/client' | ||
import { Response } from 'express' | ||
|
||
const platformFrontendUrl = process.env.PLATFORM_FRONTEND_URL | ||
const platformOAuthSuccessRedirectPath = | ||
process.env.PLATFORM_OAUTH_SUCCESS_REDIRECT_PATH | ||
const platformOAuthFailureRedirectPath = | ||
process.env.PLATFORM_OAUTH_FAILURE_REDIRECT_PATH | ||
const platformOAuthSuccessRedirectUrl = `${platformFrontendUrl}${platformOAuthSuccessRedirectPath}` | ||
const platformOAuthFailureRedirectUrl = `${platformFrontendUrl}${platformOAuthFailureRedirectPath}` | ||
|
||
/* istanbul ignore next */ | ||
export function sendOAuthFailureRedirect(response: Response, reason: string) { | ||
response | ||
.status(302) | ||
.redirect(`${platformOAuthSuccessRedirectUrl}?reason=${reason}`) | ||
} | ||
|
||
/* istanbul ignore next */ | ||
export function sendOAuthSuccessRedirect(response: Response, user: User) { | ||
response | ||
.status(302) | ||
.redirect( | ||
`${platformOAuthFailureRedirectUrl}?data=${encodeURIComponent( | ||
JSON.stringify(user) | ||
)}` | ||
) | ||
} |
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,16 @@ | ||
import { User } from '@prisma/client' | ||
import { Response } from 'express' | ||
import { UserAuthenticatedResponse } from '../auth/auth.types' | ||
|
||
/* istanbul ignore next */ | ||
export default function setCookie( | ||
response: Response, | ||
data: UserAuthenticatedResponse | ||
): User { | ||
const { token, ...user } = data | ||
response.cookie('token', `Bearer ${token}`, { | ||
domain: process.env.DOMAIN ?? 'localhost', | ||
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7) // 7 days, | ||
}) | ||
return user | ||
} |
5 changes: 5 additions & 0 deletions
5
apps/api/src/prisma/migrations/20240512141423_add_auth_provider/migration.sql
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,5 @@ | ||
-- CreateEnum | ||
CREATE TYPE "AuthProvider" AS ENUM ('GOOGLE', 'GITHUB', 'GITLAB', 'EMAIL_OTP'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "authProvider" "AuthProvider"; |
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