chore: formattato il codice e rimosso un vecchio file
This commit is contained in:
@@ -24,6 +24,14 @@ $ npm run build
|
|||||||
$ npm run serve
|
$ 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
|
# TODO
|
||||||
|
|
||||||
- [ ] Autenticazione vera magari con OAuth dell'ateneo o Google
|
- [ ] Autenticazione vera magari con OAuth dell'ateneo o Google
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
import { StateUpdater, useEffect, useState } from 'preact/hooks'
|
import { StateUpdater, useEffect, useState } from 'preact/hooks'
|
||||||
|
|
||||||
import { createContext } from 'preact'
|
import { createContext } from 'preact'
|
||||||
import { prependBaseUrl} from './api'
|
import { prependBaseUrl } from './api'
|
||||||
|
|
||||||
type Metadata = {
|
type Metadata = {
|
||||||
title?: string
|
title?: string
|
||||||
|
|||||||
+24
-22
@@ -1,4 +1,4 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express'
|
||||||
import { AuthorizationCode } from 'simple-oauth2'
|
import { AuthorizationCode } from 'simple-oauth2'
|
||||||
import fetch from 'node-fetch'
|
import fetch from 'node-fetch'
|
||||||
|
|
||||||
@@ -11,53 +11,55 @@ export function setupOauth(r: Router) {
|
|||||||
auth: {
|
auth: {
|
||||||
authorizePath: process.env.OAUTH_AUTH_URL ?? '',
|
authorizePath: process.env.OAUTH_AUTH_URL ?? '',
|
||||||
tokenHost: process.env.OAUTH_TOKEN_HOST ?? '',
|
tokenHost: process.env.OAUTH_TOKEN_HOST ?? '',
|
||||||
tokenPath: process.env.OAUTH_TOKEN_PATH ?? ''
|
tokenPath: process.env.OAUTH_TOKEN_PATH ?? '',
|
||||||
},
|
},
|
||||||
};
|
}
|
||||||
|
|
||||||
const conf = {
|
const conf = {
|
||||||
redirect_uri: process.env.OAUTH_REDIRECT_URL ?? '',
|
redirect_uri: process.env.OAUTH_REDIRECT_URL ?? '',
|
||||||
scope: process.env.OAUTH_SCOPES ?? '',
|
scope: process.env.OAUTH_SCOPES ?? '',
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new AuthorizationCode(config);
|
const client = new AuthorizationCode(config)
|
||||||
|
|
||||||
const authorizationUri = client.authorizeURL({
|
const authorizationUri = client.authorizeURL({
|
||||||
redirect_uri: conf.redirect_uri,
|
redirect_uri: conf.redirect_uri,
|
||||||
scope: conf.scope,
|
scope: conf.scope,
|
||||||
state: ''
|
state: '',
|
||||||
});
|
})
|
||||||
|
|
||||||
r.get('/redirect', (req, res) => {
|
r.get('/redirect', (req, res) => {
|
||||||
res.redirect(authorizationUri);
|
res.redirect(authorizationUri)
|
||||||
});
|
})
|
||||||
|
|
||||||
// Callback service parsing the authorization token and asking for the access token
|
// Callback service parsing the authorization token and asking for the access token
|
||||||
r.get('/callback', async (req, res) => {
|
r.get('/callback', async (req, res) => {
|
||||||
const code = req.query.code as string;
|
const code = req.query.code as string
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
code,
|
code,
|
||||||
redirect_uri: conf.redirect_uri
|
redirect_uri: conf.redirect_uri,
|
||||||
};
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const accessToken = await client.getToken(options);
|
const accessToken = await client.getToken(options)
|
||||||
|
|
||||||
console.log(accessToken.token.access_token)
|
console.log(accessToken.token.access_token)
|
||||||
|
|
||||||
const userInfo = await (await fetch(process.env.OAUTH_USER_INFO_URL ?? '', {
|
const userInfo = await (
|
||||||
method: 'GET',
|
await fetch(process.env.OAUTH_USER_INFO_URL ?? '', {
|
||||||
headers: {
|
method: 'GET',
|
||||||
'Authorization': "Bearer " + accessToken.token.access_token
|
headers: {
|
||||||
}
|
Authorization: 'Bearer ' + accessToken.token.access_token,
|
||||||
})).json()
|
},
|
||||||
|
})
|
||||||
|
).json()
|
||||||
|
|
||||||
// TODO: call to db && login
|
// TODO: call to db && login
|
||||||
return res.status(200).json(userInfo);
|
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')}`)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -229,14 +229,14 @@ export async function createApiRouter() {
|
|||||||
solutions.forEach(s => {
|
solutions.forEach(s => {
|
||||||
if (!stats[s.sentBy]) {
|
if (!stats[s.sentBy]) {
|
||||||
stats[s.sentBy] = {
|
stats[s.sentBy] = {
|
||||||
sentSolutionsCount: s.status !== "pending" ? 1 : 0,
|
sentSolutionsCount: s.status !== 'pending' ? 1 : 0,
|
||||||
correctSolutionsCount: s.status === "correct" ? 1 : 0
|
correctSolutionsCount: s.status === 'correct' ? 1 : 0,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (s.status !== "pending") {
|
if (s.status !== 'pending') {
|
||||||
stats[s.sentBy].sentSolutionsCount += 1
|
stats[s.sentBy].sentSolutionsCount += 1
|
||||||
}
|
}
|
||||||
if (s.status === "correct") {
|
if (s.status === 'correct') {
|
||||||
stats[s.sentBy].correctSolutionsCount += 1
|
stats[s.sentBy].correctSolutionsCount += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,7 +248,7 @@ export async function createApiRouter() {
|
|||||||
r.get('/api/solutions', async (req, res) => {
|
r.get('/api/solutions', async (req, res) => {
|
||||||
let queryUser = (req.query.user ?? null) as UserId | null
|
let queryUser = (req.query.user ?? null) as UserId | null
|
||||||
let queryProblem = (req.query.problem ?? null) as ProblemId | 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)
|
const requestUser = await getRequestUser(req)
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -57,8 +57,8 @@ export type SolutionStatus = 'pending' | 'correct' | 'wrong'
|
|||||||
export type SolutionId = Id<Solution>
|
export type SolutionId = Id<Solution>
|
||||||
|
|
||||||
export type SolutionStat = {
|
export type SolutionStat = {
|
||||||
sentSolutionsCount: number,
|
sentSolutionsCount: number
|
||||||
correctSolutionsCount: number,
|
correctSolutionsCount: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Solution = {
|
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)
|
return sortByKey(compareNumberKeys, items, keyFn, ascending)
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortByKey<T, K>(
|
function sortByKey<T, K>(compareFn: (a: K, b: K) => number, items: T[], keyFn: (item: T) => K, ascending: boolean) {
|
||||||
compareFn: (a: K, b: K) => number,
|
|
||||||
items: T[],
|
|
||||||
keyFn: (item: T) => K,
|
|
||||||
ascending: boolean
|
|
||||||
) {
|
|
||||||
const sortedList = [...items]
|
const sortedList = [...items]
|
||||||
const order = ascending ? 1 : -1
|
const order = ascending ? 1 : -1
|
||||||
sortedList.sort((a, b) => order * compareFn(keyFn(a), keyFn(b)))
|
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