feat: fetch user info using OAuth
This commit is contained in:
+12
-1
@@ -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
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user