-
Notifications
You must be signed in to change notification settings - Fork 1
/
pronote.js
48 lines (41 loc) · 1.66 KB
/
pronote.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require("dotenv").config();
// eslint-disable-next-line no-unused-vars
const { PronoteSession, login: sessionLogin, errors } = require("pronote-api-maintained");
module.exports = { login, logout };
/**
* Login in a PronoteApi session
* @param {Bolean} keepAlive Set if the session should last more than 10 minutes or not. (Dont forgot to `session.setKeepAlive(false)` before login out.)
*
* @returns {PronoteSession} Return a logged in PronoteAPI session.
*/
async function login(keepAlive) {
try {
const session = await sessionLogin(process.env.PRONOTE_URL, process.env.PRONOTE_USERNAME,
process.env.PRONOTE_PASSWORD, process.env.CAS ? process.env.CAS : "none");
session.setKeepAlive(keepAlive, error => {
console.log(error);
});
if (session.user !== null && session.user !== undefined) {
console.log(`Successfully logged as ${session.user.name} in ${session.user.studentClass.name}. `
+ `Session id: ${session.id} \n`);
return session;
}
} catch (err) {
if (err.code === errors.WRONG_CREDENTIALS.code) {
throw new Error("Mauvais identifiants PRONOTE.");
} else {
throw err;
}
}
}
/**
* Logout a PronoteApi session
* @param {PronoteSession} session The PronoteAPI session to logout.
*
* @param {String} taskName The name of the task that the session belongs.
*/
async function logout(session, firer) {
await session.logout().then(() => {
console.log(`Logged out of session ${session.id} of ` + firer + ".");
});
}