mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2025-10-04 02:09:24 +02:00
Remove sign in with Google account
This commit is contained in:
parent
adbd0b5a9d
commit
32d278bdd5
7 changed files with 2 additions and 277 deletions
|
@ -17,79 +17,17 @@
|
|||
|
||||
import { json } from 'body-parser'
|
||||
import { Router } from 'express'
|
||||
import { OAuth2Client } from 'google-auth-library'
|
||||
import { BadRequest } from 'http-errors'
|
||||
import { Database } from '../database'
|
||||
import { createAuthTokenByMailAddress } from '../function/authentication'
|
||||
import { sendLoginCode, signInByMailCode } from '../function/authentication/login-by-mail'
|
||||
import {
|
||||
isSendMailLoginCodeRequest,
|
||||
isSignInByMailCodeRequest,
|
||||
isSignInWithGoogleRequest
|
||||
isSignInByMailCodeRequest
|
||||
} from './validator'
|
||||
|
||||
const CLIENT_ID = process.env.GOOGLE_SIGN_IN_CLIENT_ID || ''
|
||||
const client = new OAuth2Client(CLIENT_ID)
|
||||
|
||||
const getMailByGoogleAuthToken = async (idToken: string) => {
|
||||
const ticket = await client.verifyIdToken({
|
||||
idToken,
|
||||
audience: CLIENT_ID
|
||||
})
|
||||
|
||||
if (!ticket) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
const payload = ticket.getPayload()
|
||||
|
||||
if (!payload) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
if (!payload.email_verified) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
const mail = payload.email
|
||||
|
||||
if (!mail) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
if (!(
|
||||
mail.endsWith('@gmail.com') ||
|
||||
mail.endsWith('@googlemail.com')
|
||||
)) {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
return mail
|
||||
}
|
||||
|
||||
export const createAuthRouter = (database: Database) => {
|
||||
const router = Router()
|
||||
|
||||
router.post('/sign-in-with-google', json(), async (req, res, next) => {
|
||||
try {
|
||||
if (!isSignInWithGoogleRequest(req.body)) {
|
||||
res.sendStatus(400)
|
||||
return
|
||||
}
|
||||
|
||||
const { googleAuthToken } = req.body
|
||||
|
||||
const mail = await getMailByGoogleAuthToken(googleAuthToken)
|
||||
const mailAuthToken = await createAuthTokenByMailAddress({ mail, database })
|
||||
|
||||
res.json({
|
||||
mailAuthToken
|
||||
})
|
||||
} catch (ex) {
|
||||
next(ex)
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/send-mail-login-code', json(), async (req, res, next) => {
|
||||
try {
|
||||
if (!isSendMailLoginCodeRequest(req.body)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue