Remove sign in with Google account

This commit is contained in:
Jonas Lochmann 2019-08-12 00:00:00 +00:00
parent adbd0b5a9d
commit 32d278bdd5
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
7 changed files with 2 additions and 277 deletions

View file

@ -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)) {