chore: formattato il codice e rimosso un vecchio file
This commit is contained in:
@@ -24,6 +24,14 @@ $ npm run build
|
||||
$ npm run serve
|
||||
```
|
||||
|
||||
## Contribuire
|
||||
|
||||
Plz esegui il seguente comando prima di fare commit così lo stile del codice è uniforme per tutti
|
||||
|
||||
```bash shell
|
||||
$ prettier -w '**/*.ts' '**/*.tsx'
|
||||
```
|
||||
|
||||
# TODO
|
||||
|
||||
- [ ] Autenticazione vera magari con OAuth dell'ateneo o Google
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import { StateUpdater, useEffect, useState } from 'preact/hooks'
|
||||
|
||||
import { createContext } from 'preact'
|
||||
import { prependBaseUrl} from './api'
|
||||
import { prependBaseUrl } from './api'
|
||||
|
||||
type Metadata = {
|
||||
title?: string
|
||||
|
||||
+24
-22
@@ -1,4 +1,4 @@
|
||||
import { Router } from 'express';
|
||||
import { Router } from 'express'
|
||||
import { AuthorizationCode } from 'simple-oauth2'
|
||||
import fetch from 'node-fetch'
|
||||
|
||||
@@ -11,53 +11,55 @@ export function setupOauth(r: Router) {
|
||||
auth: {
|
||||
authorizePath: process.env.OAUTH_AUTH_URL ?? '',
|
||||
tokenHost: process.env.OAUTH_TOKEN_HOST ?? '',
|
||||
tokenPath: process.env.OAUTH_TOKEN_PATH ?? ''
|
||||
tokenPath: process.env.OAUTH_TOKEN_PATH ?? '',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const conf = {
|
||||
redirect_uri: process.env.OAUTH_REDIRECT_URL ?? '',
|
||||
scope: process.env.OAUTH_SCOPES ?? '',
|
||||
}
|
||||
|
||||
const client = new AuthorizationCode(config);
|
||||
const client = new AuthorizationCode(config)
|
||||
|
||||
const authorizationUri = client.authorizeURL({
|
||||
redirect_uri: conf.redirect_uri,
|
||||
scope: conf.scope,
|
||||
state: ''
|
||||
});
|
||||
state: '',
|
||||
})
|
||||
|
||||
r.get('/redirect', (req, res) => {
|
||||
res.redirect(authorizationUri);
|
||||
});
|
||||
res.redirect(authorizationUri)
|
||||
})
|
||||
|
||||
// Callback service parsing the authorization token and asking for the access token
|
||||
r.get('/callback', async (req, res) => {
|
||||
const code = req.query.code as string;
|
||||
const code = req.query.code as string
|
||||
|
||||
const options = {
|
||||
code,
|
||||
redirect_uri: conf.redirect_uri
|
||||
};
|
||||
redirect_uri: conf.redirect_uri,
|
||||
}
|
||||
|
||||
try {
|
||||
const accessToken = await client.getToken(options);
|
||||
const accessToken = await client.getToken(options)
|
||||
|
||||
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()
|
||||
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);
|
||||
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')}`);
|
||||
console.error('Access Token Error', error.message)
|
||||
return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`)
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
+5
-5
@@ -229,14 +229,14 @@ export async function createApiRouter() {
|
||||
solutions.forEach(s => {
|
||||
if (!stats[s.sentBy]) {
|
||||
stats[s.sentBy] = {
|
||||
sentSolutionsCount: s.status !== "pending" ? 1 : 0,
|
||||
correctSolutionsCount: s.status === "correct" ? 1 : 0
|
||||
sentSolutionsCount: s.status !== 'pending' ? 1 : 0,
|
||||
correctSolutionsCount: s.status === 'correct' ? 1 : 0,
|
||||
}
|
||||
} else {
|
||||
if (s.status !== "pending") {
|
||||
if (s.status !== 'pending') {
|
||||
stats[s.sentBy].sentSolutionsCount += 1
|
||||
}
|
||||
if (s.status === "correct") {
|
||||
if (s.status === 'correct') {
|
||||
stats[s.sentBy].correctSolutionsCount += 1
|
||||
}
|
||||
}
|
||||
@@ -248,7 +248,7 @@ export async function createApiRouter() {
|
||||
r.get('/api/solutions', async (req, res) => {
|
||||
let queryUser = (req.query.user ?? null) as UserId | null
|
||||
let queryProblem = (req.query.problem ?? null) as ProblemId | null
|
||||
let isPublic = (req.query.public === '')
|
||||
let isPublic = req.query.public === ''
|
||||
|
||||
const requestUser = await getRequestUser(req)
|
||||
|
||||
|
||||
+2
-2
@@ -57,8 +57,8 @@ export type SolutionStatus = 'pending' | 'correct' | 'wrong'
|
||||
export type SolutionId = Id<Solution>
|
||||
|
||||
export type SolutionStat = {
|
||||
sentSolutionsCount: number,
|
||||
correctSolutionsCount: number,
|
||||
sentSolutionsCount: number
|
||||
correctSolutionsCount: number
|
||||
}
|
||||
|
||||
export type Solution = {
|
||||
|
||||
+1
-6
@@ -22,12 +22,7 @@ export function sortByNumericKey<T>(items: T[], keyFn: KeyFn<T, number>, ascendi
|
||||
return sortByKey(compareNumberKeys, items, keyFn, ascending)
|
||||
}
|
||||
|
||||
function sortByKey<T, K>(
|
||||
compareFn: (a: K, b: K) => number,
|
||||
items: T[],
|
||||
keyFn: (item: T) => K,
|
||||
ascending: boolean
|
||||
) {
|
||||
function sortByKey<T, K>(compareFn: (a: K, b: K) => number, items: T[], keyFn: (item: T) => K, ascending: boolean) {
|
||||
const sortedList = [...items]
|
||||
const order = ascending ? 1 : -1
|
||||
sortedList.sort((a, b) => order * compareFn(keyFn(a), keyFn(b)))
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
export function toLocalISO(date) {
|
||||
const tzo = -date.getTimezoneOffset()
|
||||
const dif = tzo >= 0 ? '+' : '-'
|
||||
const pad = function (num) {
|
||||
return num.toString().padStart(2, '0')
|
||||
}
|
||||
|
||||
return (
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
pad(date.getMonth() + 1) +
|
||||
'-' +
|
||||
pad(date.getDate()) +
|
||||
'T' +
|
||||
pad(date.getHours()) +
|
||||
':' +
|
||||
pad(date.getMinutes()) +
|
||||
':' +
|
||||
pad(date.getSeconds()) +
|
||||
dif +
|
||||
pad(Math.floor(Math.abs(tzo) / 60)) +
|
||||
':' +
|
||||
pad(Math.abs(tzo) % 60)
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user