-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2c3abd
commit 81b1bf1
Showing
19 changed files
with
423 additions
and
38 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 |
---|---|---|
|
@@ -13,10 +13,13 @@ | |
}, | ||
"imports": { | ||
"@/": "./src/", | ||
"@std/encoding": "jsr:@std/encoding@^1.0.5", | ||
"@std/assert": "jsr:@std/assert@^1.0.8", | ||
"@std/collections": "jsr:@std/collections@^1.0.9", | ||
"@std/text": "jsr:@std/text@^1.0.8", | ||
"@vighnesh153/tools": "jsr:@vighnesh153/[email protected]", | ||
"firebase-admin": "npm:firebase-admin@^13.0.1", | ||
"hono": "jsr:@hono/[email protected]" | ||
"hono": "jsr:@hono/[email protected]", | ||
"zod": "npm:zod@^3.23.8" | ||
}, | ||
"nodeModulesDir": "auto" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
tools-deno/api-vighnesh153/src/api/google_auth_callback/build_token_fetch_request.ts
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,20 @@ | ||
import { JsonHttpPostRequest } from "@vighnesh153/tools"; | ||
import { config } from "@/config.ts"; | ||
import { serverAuthRedirectUrl } from "@/constants.ts"; | ||
|
||
export function buildTokenFetchRequest( | ||
{ authCallbackCode }: { authCallbackCode: string }, | ||
): JsonHttpPostRequest<FormData> { | ||
const formData = new FormData(); | ||
|
||
formData.append("code", authCallbackCode); | ||
formData.append("client_id", config.googleClientId); | ||
formData.append("client_secret", config.googleClientSecret); | ||
formData.append("grant_type", "authorization_code"); | ||
formData.append("redirect_uri", serverAuthRedirectUrl); | ||
|
||
return { | ||
path: "https://oauth2.googleapis.com/token", | ||
data: formData, | ||
}; | ||
} |
95 changes: 95 additions & 0 deletions
95
tools-deno/api-vighnesh153/src/api/google_auth_callback/controller.ts
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,95 @@ | ||
import { JsonHttpClient, JsonHttpClientImpl, not } from "@vighnesh153/tools"; | ||
import { decodeUserInfo } from "./decode_user_info.ts"; | ||
import { UserRepository } from "@/repositories/mod.ts"; | ||
import { | ||
authTokenGeneratorFactory, | ||
userRepositoryFactory, | ||
} from "@/factories/mod.ts"; | ||
import { AuthTokenGenerator } from "@/utils/auth_token_generator.ts"; | ||
import { CompleteUserInfo } from "@/models/user_info.ts"; | ||
import { buildTokenFetchRequest } from "./build_token_fetch_request.ts"; | ||
|
||
type ControllerResponse = { success: false } | { | ||
success: true; | ||
user: CompleteUserInfo; | ||
authToken: string; | ||
}; | ||
|
||
export async function googleAuthCallbackController( | ||
{ | ||
authCallbackCode = "", | ||
httpClient = new JsonHttpClientImpl({ baseUrl: "" }), | ||
userRepository = userRepositoryFactory(), | ||
authTokenGenerator = authTokenGeneratorFactory(), | ||
}: { | ||
authCallbackCode?: string; | ||
httpClient?: JsonHttpClient; | ||
userRepository?: UserRepository; | ||
authTokenGenerator?: AuthTokenGenerator; | ||
} = {}, | ||
): Promise<ControllerResponse> { | ||
const tokenFetchRequest = buildTokenFetchRequest({ authCallbackCode }); | ||
|
||
const tokenFetcher = httpClient.post<unknown, { id_token: string }>( | ||
tokenFetchRequest, | ||
); | ||
|
||
console.log("Fetching google auth token..."); | ||
const tokenResponse = await tokenFetcher.execute(); | ||
|
||
if (tokenResponse.isError()) { | ||
console.log("Some error occurred while fetching google auth token"); | ||
console.log(tokenResponse.getErrorResponse()); | ||
return { | ||
success: false, | ||
}; | ||
} | ||
|
||
console.log("Google auth token fetch is successful"); | ||
|
||
const tokenData = tokenResponse.getSuccessResponse(); | ||
|
||
// extract user info from token | ||
console.log("Extracting user info from token"); | ||
const oauthUserInfo = decodeUserInfo(tokenData.data.id_token); | ||
|
||
if (oauthUserInfo === null) { | ||
console.log("Failed to extract user info from token"); | ||
console.log(`token=${tokenData.data.id_token}`); | ||
return { success: false }; | ||
} | ||
|
||
console.log("Successfully extracted user info from token"); | ||
|
||
// user's email is not verified. deny signing in | ||
if (not(oauthUserInfo.email_verified)) { | ||
console.log(`User's email address is not verified`); | ||
console.log(oauthUserInfo); | ||
return { success: false }; | ||
} | ||
|
||
console.log(`User's email address is verified`); | ||
|
||
console.log("Attempting to creating or getting user..."); | ||
const loggedInUser = await userRepository.createOrGetUser(oauthUserInfo); | ||
|
||
if (loggedInUser == null) { | ||
console.log( | ||
"Failed to create or get user... Failing sign up or sign in...", | ||
); | ||
return { success: false }; | ||
} | ||
|
||
console.log("Generating auth token..."); | ||
const authToken = authTokenGenerator.generate({ | ||
userId: loggedInUser.userId, | ||
}); | ||
|
||
console.log("Generated auth token and login user complete..."); | ||
|
||
return { | ||
success: true, | ||
user: loggedInUser, | ||
authToken, | ||
}; | ||
} |
16 changes: 16 additions & 0 deletions
16
tools-deno/api-vighnesh153/src/api/google_auth_callback/decode_user_info.ts
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 { GoogleOAuthUserInfo } from "@/models/user_info.ts"; | ||
|
||
export function decodeUserInfo(token: string): GoogleOAuthUserInfo | null { | ||
try { | ||
const json = atob(token.split(".")[1]); | ||
const parsedResult = GoogleOAuthUserInfo.safeParse(JSON.parse(json)); | ||
if (parsedResult.success) { | ||
return parsedResult.data; | ||
} | ||
throw parsedResult.error; | ||
} catch (e) { | ||
console.log(`Some error occurred while parsing Google Oauth token`); | ||
console.log(e); | ||
return null; | ||
} | ||
} |
11 changes: 0 additions & 11 deletions
11
tools-deno/api-vighnesh153/src/api/initiateGoogleLogin/controller.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
8 changes: 8 additions & 0 deletions
8
tools-deno/api-vighnesh153/src/api/initiate_google_login/controller.ts
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,8 @@ | ||
import { serverAuthRedirectUrl } from "@/constants.ts"; | ||
import { constructInitiateGoogleAuthUrl } from "./construct_initiate_google_auth_url.ts"; | ||
|
||
export function initiateGoogleLoginController(): string | null { | ||
return constructInitiateGoogleAuthUrl({ | ||
authRedirectUri: serverAuthRedirectUrl, | ||
}); | ||
} |
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
9 changes: 9 additions & 0 deletions
9
tools-deno/api-vighnesh153/src/factories/auth_token_generator_factory.ts
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,9 @@ | ||
import { createSingletonFactory } from "@vighnesh153/tools"; | ||
import { | ||
AuthTokenGenerator, | ||
AuthTokenGeneratorImpl, | ||
} from "@/utils/auth_token_generator.ts"; | ||
|
||
export const authTokenGeneratorFactory = createSingletonFactory< | ||
AuthTokenGenerator | ||
>(() => new AuthTokenGeneratorImpl()); |
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,2 @@ | ||
export * from "./auth_token_generator_factory.ts"; | ||
export * from "./user_repository_factory.ts"; |
6 changes: 6 additions & 0 deletions
6
tools-deno/api-vighnesh153/src/factories/user_repository_factory.ts
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,6 @@ | ||
import { createSingletonFactory } from "@vighnesh153/tools"; | ||
import { FirebaseUserRepository, UserRepository } from "@/repositories/mod.ts"; | ||
|
||
export const userRepositoryFactory = createSingletonFactory<UserRepository>( | ||
() => new FirebaseUserRepository(), | ||
); |
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,33 @@ | ||
import { z } from "zod"; | ||
import { filterKeys } from "@std/collections"; | ||
|
||
export const GoogleOAuthUserInfo = z.object({ | ||
name: z.string().min(1), | ||
email: z.string().email(), | ||
picture: z.string().min(1), | ||
email_verified: z.boolean(), | ||
}); | ||
export type GoogleOAuthUserInfo = z.infer<typeof GoogleOAuthUserInfo>; | ||
|
||
export const PublicUserInfo = z.object({ | ||
userId: z.string().min(1), | ||
username: z.string().min(1), | ||
name: z.string().min(1), | ||
profilePictureUrl: z.string().min(1), | ||
createdAtMillis: z.number(), | ||
}); | ||
export type PublicUserInfo = z.infer<typeof PublicUserInfo>; | ||
|
||
export const CompleteUserInfo = PublicUserInfo.extend({ | ||
email: z.string().email(), | ||
}); | ||
export type CompleteUserInfo = z.infer<typeof CompleteUserInfo>; | ||
|
||
export function convertToPublicUserInfo( | ||
completeUserInfo: CompleteUserInfo, | ||
): PublicUserInfo { | ||
return filterKeys( | ||
completeUserInfo, | ||
(field) => field != "email", | ||
) as PublicUserInfo; | ||
} |
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 @@ | ||
export * from "./user_repository.ts"; |
Oops, something went wrong.