feat: fetch user info using OAuth

This commit is contained in:
2023-01-10 01:22:06 +01:00
parent 3d149e0d1c
commit d279e1468b
5 changed files with 57 additions and 5 deletions
+12 -1
View File
@@ -1,5 +1,6 @@
import { Router } from 'express';
import { AuthorizationCode } from 'simple-oauth2'
import fetch from 'node-fetch'
export function setupOauth(r: Router) {
const config = {
@@ -43,7 +44,17 @@ export function setupOauth(r: Router) {
try {
const accessToken = await client.getToken(options);
return res.status(200).json(accessToken.token);
console.log(accessToken.token.access_token)
const userInfo = await (await fetch(process.env.OAUTH_USER_INFO_URL ?? '', {
method: 'GET',
headers: {
'Authorization': "Bearer " + accessToken.token.access_token
}
})).json()
// TODO: call to db && login
return res.status(200).json(userInfo);
} catch (error) {
console.error('Access Token Error', error.message);
return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`);
+1 -1
View File
@@ -85,7 +85,7 @@ export async function createApiRouter() {
})
r.post('/api/login', async (req, res) => {
const { id } = req.body
const { id, name } = req.body
const user = await getUser(db, id)
if (!user) {