feat: fetch user info using OAuth

pull/1/head
Francesco Minnocci 2 years ago
parent 3d149e0d1c
commit d279e1468b
Signed by: BachoSeven
GPG Key ID: 2BE4AB7FDAD828A4

@ -1,8 +1,7 @@
import { StateUpdater, useContext, useEffect, useState } from 'preact/hooks' import { StateUpdater, useEffect, useState } from 'preact/hooks'
import { createContext } from 'preact' import { createContext } from 'preact'
import { prependBaseUrl, server } from './api' import { prependBaseUrl} from './api'
import { User } from '../shared/model'
type Metadata = { type Metadata = {
title?: string title?: string

@ -25,6 +25,7 @@
"http-status-codes": "^2.2.0", "http-status-codes": "^2.2.0",
"katex": "^0.16.3", "katex": "^0.16.3",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"node-fetch": "^3.3.0",
"preact": "^10.11.2", "preact": "^10.11.2",
"preact-render-to-string": "^5.2.6", "preact-render-to-string": "^5.2.6",
"preact-router": "^4.1.0", "preact-router": "^4.1.0",

@ -18,6 +18,7 @@ specifiers:
http-status-codes: ^2.2.0 http-status-codes: ^2.2.0
katex: ^0.16.3 katex: ^0.16.3
morgan: ^1.10.0 morgan: ^1.10.0
node-fetch: ^3.3.0
npm-run-all: ^4.1.5 npm-run-all: ^4.1.5
preact: ^10.11.2 preact: ^10.11.2
preact-render-to-string: ^5.2.6 preact-render-to-string: ^5.2.6
@ -46,6 +47,7 @@ dependencies:
http-status-codes: 2.2.0 http-status-codes: 2.2.0
katex: 0.16.3 katex: 0.16.3
morgan: 1.10.0 morgan: 1.10.0
node-fetch: 3.3.0
preact: 10.11.2 preact: 10.11.2
preact-render-to-string: 5.2.6_preact@10.11.2 preact-render-to-string: 5.2.6_preact@10.11.2
preact-router: 4.1.0_preact@10.11.2 preact-router: 4.1.0_preact@10.11.2
@ -924,6 +926,11 @@ packages:
which: 1.3.1 which: 1.3.1
dev: true dev: true
/data-uri-to-buffer/4.0.0:
resolution: {integrity: sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==}
engines: {node: '>= 12'}
dev: false
/date-fns/2.29.3: /date-fns/2.29.3:
resolution: {integrity: sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==} resolution: {integrity: sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==}
engines: {node: '>=0.11'} engines: {node: '>=0.11'}
@ -1311,6 +1318,14 @@ packages:
resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==} resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==}
dev: false dev: false
/fetch-blob/3.2.0:
resolution: {integrity: sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==}
engines: {node: ^12.20 || >= 14.13}
dependencies:
node-domexception: 1.0.0
web-streams-polyfill: 3.2.1
dev: false
/fill-range/7.0.1: /fill-range/7.0.1:
resolution: {integrity: sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==} resolution: {integrity: sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==}
engines: {node: '>=8'} engines: {node: '>=8'}
@ -1333,6 +1348,13 @@ packages:
- supports-color - supports-color
dev: false dev: false
/formdata-polyfill/4.0.10:
resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==}
engines: {node: '>=12.20.0'}
dependencies:
fetch-blob: 3.2.0
dev: false
/forwarded/0.2.0: /forwarded/0.2.0:
resolution: {integrity: sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==} resolution: {integrity: sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==}
engines: {node: '>= 0.6'} engines: {node: '>= 0.6'}
@ -2107,6 +2129,20 @@ packages:
resolution: {integrity: sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==} resolution: {integrity: sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==}
dev: true dev: true
/node-domexception/1.0.0:
resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==}
engines: {node: '>=10.5.0'}
dev: false
/node-fetch/3.3.0:
resolution: {integrity: sha512-BKwRP/O0UvoMKp7GNdwPlObhYGB5DQqwhEDQlNKuoqwVYSxkSZCSbHjnFFmUEtwSKRPU4kNK8PbDYYitwaE3QA==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
dependencies:
data-uri-to-buffer: 4.0.0
fetch-blob: 3.2.0
formdata-polyfill: 4.0.10
dev: false
/node-releases/2.0.6: /node-releases/2.0.6:
resolution: {integrity: sha512-PiVXnNuFm5+iYkLBNeq5211hvO38y63T0i2KKh2KnUs3RpzJ+JtODFjkD8yjLwnDkTYF1eKXheUwdssR+NRZdg==} resolution: {integrity: sha512-PiVXnNuFm5+iYkLBNeq5211hvO38y63T0i2KKh2KnUs3RpzJ+JtODFjkD8yjLwnDkTYF1eKXheUwdssR+NRZdg==}
dev: false dev: false
@ -2913,6 +2949,11 @@ packages:
resolution: {integrity: sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ==} resolution: {integrity: sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ==}
dev: false dev: false
/web-streams-polyfill/3.2.1:
resolution: {integrity: sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==}
engines: {node: '>= 8'}
dev: false
/which-boxed-primitive/1.0.2: /which-boxed-primitive/1.0.2:
resolution: {integrity: sha512-bwZdv0AKLpplFY2KZRX6TvyuN7ojjr7lwkg6ml0roIy9YeuSr7JS372qlNW18UQYzgYK9ziGcerWqZOmEn9VNg==} resolution: {integrity: sha512-bwZdv0AKLpplFY2KZRX6TvyuN7ojjr7lwkg6ml0roIy9YeuSr7JS372qlNW18UQYzgYK9ziGcerWqZOmEn9VNg==}
dependencies: dependencies:

@ -1,5 +1,6 @@
import { Router } from 'express'; import { Router } from 'express';
import { AuthorizationCode } from 'simple-oauth2' import { AuthorizationCode } from 'simple-oauth2'
import fetch from 'node-fetch'
export function setupOauth(r: Router) { export function setupOauth(r: Router) {
const config = { const config = {
@ -43,7 +44,17 @@ export function setupOauth(r: Router) {
try { try {
const accessToken = await client.getToken(options); 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) { } catch (error) {
console.error('Access Token Error', error.message); console.error('Access Token Error', error.message);
return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`); return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`);

@ -85,7 +85,7 @@ export async function createApiRouter() {
}) })
r.post('/api/login', async (req, res) => { r.post('/api/login', async (req, res) => {
const { id } = req.body const { id, name } = req.body
const user = await getUser(db, id) const user = await getUser(db, id)
if (!user) { if (!user) {

Loading…
Cancel
Save