style: fix linting issues met ESLint
This commit is contained in:
parent
834ff236aa
commit
394deba56d
3 changed files with 14 additions and 14 deletions
|
@ -12,7 +12,7 @@ const JWKS_CACHE = true;
|
||||||
const JWKS_RATE_LIMIT = true;
|
const JWKS_RATE_LIMIT = true;
|
||||||
const REQUEST_PROPERTY_FOR_JWT_PAYLOAD = "jwtPayload";
|
const REQUEST_PROPERTY_FOR_JWT_PAYLOAD = "jwtPayload";
|
||||||
const JWT_ALGORITHM = "RS256"; // Not configurable via env vars since supporting other algorithms would
|
const JWT_ALGORITHM = "RS256"; // Not configurable via env vars since supporting other algorithms would
|
||||||
// require additional libraries to be added.
|
// Require additional libraries to be added.
|
||||||
|
|
||||||
const JWT_PROPERTY_NAMES = {
|
const JWT_PROPERTY_NAMES = {
|
||||||
username: "preferred_username",
|
username: "preferred_username",
|
||||||
|
@ -50,9 +50,9 @@ const verifyJwtToken = expressjwt({
|
||||||
throw new Error("Invalid token");
|
throw new Error("Invalid token");
|
||||||
}
|
}
|
||||||
|
|
||||||
let issuer = (token.payload as JwtPayload).iss;
|
const issuer = (token.payload as JwtPayload).iss;
|
||||||
|
|
||||||
let idpConfig = Object.values(idpConfigs).find(config => config.issuer === issuer);
|
const idpConfig = Object.values(idpConfigs).find(config => {return config.issuer === issuer});
|
||||||
if (!idpConfig) {
|
if (!idpConfig) {
|
||||||
throw new Error("Issuer not accepted.");
|
throw new Error("Issuer not accepted.");
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ function getAuthenticationInfo(req: AuthenticatedRequest): AuthenticationInfo |
|
||||||
if (!req.jwtPayload) {
|
if (!req.jwtPayload) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let issuer = req.jwtPayload.iss;
|
const issuer = req.jwtPayload.iss;
|
||||||
let accountType: "student" | "teacher";
|
let accountType: "student" | "teacher";
|
||||||
|
|
||||||
if (issuer === idpConfigs.student.issuer) {
|
if (issuer === idpConfigs.student.issuer) {
|
||||||
|
@ -128,14 +128,14 @@ export const authorize = (accessCondition: (auth: AuthenticationInfo) => boolean
|
||||||
/**
|
/**
|
||||||
* Middleware which rejects all unauthenticated users, but accepts all authenticated users.
|
* Middleware which rejects all unauthenticated users, but accepts all authenticated users.
|
||||||
*/
|
*/
|
||||||
export const authenticatedOnly = authorize(_ => true);
|
export const authenticatedOnly = authorize(_ => {return true});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Middleware which rejects requests from unauthenticated users or users that aren't students.
|
* Middleware which rejects requests from unauthenticated users or users that aren't students.
|
||||||
*/
|
*/
|
||||||
export const studentsOnly = authorize(auth => auth.accountType === "student");
|
export const studentsOnly = authorize(auth => {return auth.accountType === "student"});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Middleware which rejects requests from unauthenticated users or users that aren't teachers.
|
* Middleware which rejects requests from unauthenticated users or users that aren't teachers.
|
||||||
*/
|
*/
|
||||||
export const teachersOnly = authorize(auth => auth.accountType === "teacher");
|
export const teachersOnly = authorize(auth => {return auth.accountType === "teacher"});
|
||||||
|
|
|
@ -3,7 +3,7 @@ import {getFrontendAuthConfig} from "../controllers/auth.js";
|
||||||
import {authenticatedOnly, studentsOnly, teachersOnly} from "../middleware/auth/auth.js";
|
import {authenticatedOnly, studentsOnly, teachersOnly} from "../middleware/auth/auth.js";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// returns auth configuration for frontend
|
// Returns auth configuration for frontend
|
||||||
router.get('/config', (req, res) => {
|
router.get('/config', (req, res) => {
|
||||||
res.json(getFrontendAuthConfig());
|
res.json(getFrontendAuthConfig());
|
||||||
});
|
});
|
||||||
|
|
|
@ -27,7 +27,7 @@ async function loadUser(): Promise<User | null> {
|
||||||
if (!activeRole) {
|
if (!activeRole) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
let user = await userManagers[activeRole].getUser();
|
const user = await userManagers[activeRole].getUser();
|
||||||
authState.user = user;
|
authState.user = user;
|
||||||
authState.accessToken = user?.access_token || null;
|
authState.accessToken = user?.access_token || null;
|
||||||
authState.activeRole = activeRole || null;
|
authState.activeRole = activeRole || null;
|
||||||
|
@ -43,7 +43,7 @@ const authState = reactive<AuthState>({
|
||||||
activeRole: authStorage.getActiveRole() || null
|
activeRole: authStorage.getActiveRole() || null
|
||||||
});
|
});
|
||||||
|
|
||||||
const isLoggedIn = computed(() => authState.user !== null);
|
const isLoggedIn = computed(() => {return authState.user !== null});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redirect the user to the login page where he/she can choose whether to log in as a student or teacher.
|
* Redirect the user to the login page where he/she can choose whether to log in as a student or teacher.
|
||||||
|
@ -110,20 +110,20 @@ apiClient.interceptors.request.use(async (reqConfig) => {
|
||||||
reqConfig.headers.Authorization = `Bearer ${token}`;
|
reqConfig.headers.Authorization = `Bearer ${token}`;
|
||||||
}
|
}
|
||||||
return reqConfig;
|
return reqConfig;
|
||||||
}, (error) => Promise.reject(error));
|
}, (error) => {return Promise.reject(error)});
|
||||||
|
|
||||||
// Registering interceptor to refresh the token when a request failed because it was expired.
|
// Registering interceptor to refresh the token when a request failed because it was expired.
|
||||||
apiClient.interceptors.response.use(
|
apiClient.interceptors.response.use(
|
||||||
response => response,
|
response => {return response},
|
||||||
async (error: AxiosError<{message?: string}>) => {
|
async (error: AxiosError<{message?: string}>) => {
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
if (error.response!.data.message === "token_expired") {
|
if (error.response!.data.message === "token_expired") {
|
||||||
console.log("Access token expired, trying to refresh...");
|
console.log("Access token expired, trying to refresh...");
|
||||||
await renewToken();
|
await renewToken();
|
||||||
return apiClient(error.config!); // Retry the request
|
return apiClient(error.config!); // Retry the request
|
||||||
} else { // Apparently, the user got a 401 because he was not logged in yet at all. Redirect him to login.
|
} // Apparently, the user got a 401 because he was not logged in yet at all. Redirect him to login.
|
||||||
await initiateLogin()
|
await initiateLogin()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue