2020-12-19 10:37:37 +00:00
|
|
|
# embedded-api
|
2020-12-19 12:37:45 +00:00
|
|
|
|
2020-12-19 10:37:37 +00:00
|
|
|
Since the proxy was blocked (and also deemed a bad idea by some), this is a reboot of the API running in process in the app(s).
|
2020-12-20 22:02:05 +00:00
|
|
|
|
2020-12-21 08:51:51 +00:00
|
|
|
## Installing
|
2020-12-20 22:02:05 +00:00
|
|
|
|
|
|
|
`npm i -S @skolplattformen/embedded-api` or `yarn add @skolplattformen/embedded-api`
|
|
|
|
|
2020-12-21 08:51:51 +00:00
|
|
|
## Calling
|
|
|
|
|
|
|
|
### Import and init
|
|
|
|
|
|
|
|
Since fetch and cookies behave distinctly different in node, react-native and the browser,
|
|
|
|
the concrete implementation of fetch and cookie handler must be injected.
|
2020-12-20 22:02:05 +00:00
|
|
|
|
2020-12-21 08:51:51 +00:00
|
|
|
#### react-native
|
2020-12-20 22:02:05 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
import init from "@skolplattformen/embedded-api";
|
2020-12-21 08:51:51 +00:00
|
|
|
import CookieManager from "@react-native-community/cookies";
|
2020-12-20 22:02:05 +00:00
|
|
|
|
2020-12-21 08:51:51 +00:00
|
|
|
const api = init(fetch, () => CookieManager.clearAll());
|
2020-12-20 22:02:05 +00:00
|
|
|
```
|
|
|
|
|
2020-12-21 08:51:51 +00:00
|
|
|
#### node
|
2020-12-20 22:02:05 +00:00
|
|
|
|
|
|
|
```javascript
|
2020-12-21 08:51:51 +00:00
|
|
|
import init from "@skolplattformen/embedded-api";
|
|
|
|
import nodeFetch from "node-fetch";
|
|
|
|
import fetchCookie from "fetch-cookie/node-fetch";
|
|
|
|
import { CookieJar } from "tough-cookie";
|
|
|
|
|
|
|
|
const cookieJar = new CookieJar();
|
|
|
|
const fetch = fetchCookie(nodeFetch, cookieJar);
|
|
|
|
|
|
|
|
const api = init(fetch, () => cookieJar.removeAllCookies());
|
|
|
|
```
|
|
|
|
|
|
|
|
### Login / logout
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
api.on("login", async () => {
|
|
|
|
// do stuff
|
|
|
|
console.log(api.isLoggedIn) // true
|
2020-12-21 19:09:21 +00:00
|
|
|
console.log(api.getSessionCookie) // session cookie if you want to save it
|
2020-12-21 08:51:51 +00:00
|
|
|
await api.logout()
|
2020-12-20 22:02:05 +00:00
|
|
|
});
|
2020-12-21 08:51:51 +00:00
|
|
|
api.on('logout', () => {
|
|
|
|
// handle logout
|
|
|
|
console.log(api.isLoggedIn) // false
|
|
|
|
}
|
2020-12-20 22:02:05 +00:00
|
|
|
|
|
|
|
const loginStatus = await api.login("YYYYMMDDXXXX");
|
2020-12-21 08:51:51 +00:00
|
|
|
window.open(
|
|
|
|
`https://app.bankid.com/?autostarttoken=${loginStatus.token}&redirect=null`
|
|
|
|
);
|
2020-12-21 01:28:26 +00:00
|
|
|
|
|
|
|
loginStatus.on("PENDING", () => console.log("BankID app not yet opened"));
|
|
|
|
loginStatus.on("USER_SIGN", () => console.log("BankID app is open"));
|
|
|
|
loginStatus.on("ERROR", () => console.log("Something went wrong"));
|
2020-12-21 08:51:51 +00:00
|
|
|
loginStatus.on("OK", () =>
|
2020-12-20 22:02:05 +00:00
|
|
|
console.log("BankID sign successful. Session will be established.")
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2020-12-21 08:51:51 +00:00
|
|
|
### Loading data
|
2020-12-20 22:02:05 +00:00
|
|
|
|
|
|
|
```javascript
|
2020-12-21 16:01:22 +00:00
|
|
|
// Get current user
|
|
|
|
const user = await api.getUser();
|
|
|
|
|
2020-12-20 22:02:05 +00:00
|
|
|
// List children
|
|
|
|
const children = await api.getChildren();
|
|
|
|
|
|
|
|
// Get calendar
|
2020-12-21 15:06:09 +00:00
|
|
|
const calendar = await api.getCalendar(children[0]);
|
|
|
|
|
|
|
|
// Get classmates
|
|
|
|
const classmates = await api.getClassmates(children[0]);
|
|
|
|
|
|
|
|
// Get schedule
|
2021-01-05 13:13:27 +00:00
|
|
|
import { DateTime } from 'luxon'
|
|
|
|
|
|
|
|
const from = DateTime.local()
|
|
|
|
const to = DateTime.local().plus({ week: 1 })
|
2020-12-21 15:06:09 +00:00
|
|
|
const schedule = await api.getSchedule(children[0], from, to)
|
|
|
|
|
|
|
|
// Get news
|
|
|
|
const news = await api.getNews(children[0])
|
2020-12-21 15:32:02 +00:00
|
|
|
|
|
|
|
// Get menu
|
|
|
|
const menu = await api.getMenu(children[0])
|
2020-12-21 17:55:38 +00:00
|
|
|
|
|
|
|
// Get notifications
|
|
|
|
const notifications = await api.getNotifications(children[0])
|
2020-12-20 22:02:05 +00:00
|
|
|
```
|
2020-12-21 08:51:51 +00:00
|
|
|
|
|
|
|
### Setting session cookie
|
|
|
|
|
|
|
|
It is possible to resurrect a logged in session by manually setting the session cookie.
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
const sessionCookie = "some value";
|
|
|
|
|
|
|
|
api.setSessionCookie(sessionCookie); // will trigger `on('login')` event and set `.isLoggedIn = true`
|
|
|
|
```
|
2020-12-21 14:59:49 +00:00
|
|
|
|
2021-01-06 22:46:45 +00:00
|
|
|
### Fake user
|
|
|
|
|
|
|
|
Login with personal number "12121212121212" and api will be put into fake mode.
|
|
|
|
Static data will be returned and no calls to backend will be made.
|
|
|
|
|
|
|
|
The `LoginStatusChecker` returned by the login method will have `.token` set to "fake".
|
|
|
|
|
2020-12-21 14:59:49 +00:00
|
|
|
## Try it out
|
|
|
|
|
|
|
|
1. Clone and enter repo: `git clone git@github.com:kolplattformen/embedded-api.git && cd embedded-api`
|
|
|
|
2. Install dependencies: `yarn`
|
|
|
|
3. Build package: `yarn build`
|
|
|
|
4. Run example: `node run [your personal number]`
|
|
|
|
5. Sign in with mobile BankID
|