Skip to content

Commit a21407f

Browse files
author
Ondřej Baše
committed
feat: 🎸 Login plugin
1 parent 319aeb0 commit a21407f

File tree

11 files changed

+766
-0
lines changed

11 files changed

+766
-0
lines changed

Diff for: packages/plugin-login/.npmignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/src
2+
/coverage
3+
.babelrc
4+
.eslintrc.js
5+
.gitignore
6+
.npmignore
7+
.npmrc
8+
.prettierignore
9+
.travis.yml
10+
.DS_Store
11+
README.md
12+
CHANGELOG.md
13+
commitlint.config.js
14+
rollup.config.js
15+
babel.config.js
16+
jest.config.js
17+
setupJest.js

Diff for: packages/plugin-login/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry = "https://registry.npmjs.org/"

Diff for: packages/plugin-login/CHANGELOG.md

Whitespace-only changes.

Diff for: packages/plugin-login/LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2023 Miroslav Jancarik [email protected]
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: packages/plugin-login/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Merkur - plugin-login
2+
3+
[![Build Status](https://github.com/mjancarik/merkur/workflows/CI/badge.svg)](https://travis-ci.com/mjancarik/merkur)
4+
[![NPM package version](https://img.shields.io/npm/v/@merkur/plugin-login/latest.svg)](https://www.npmjs.com/package/@merkur/plugin-login)
5+
![npm bundle size (scoped version)](https://img.shields.io/bundlephobia/minzip/@merkur/plugin-login/latest)
6+
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
7+
8+
The Login plugin adds `login` property to your widget with the `listenToUserEvents` and `unlistenUserEvents` methods. On the server side, these methods do nothing. It has two dependencies: [API Login JS library version 3+](https://gitlab.seznam.net/rus/login/-/wikis/widget#js-api-knihovny) and Merkur Session Storage plugin.
9+
10+
## About Merkur
11+
12+
The [Merkur](https://merkur.js.org/) is tiny extensible javascript library for front-end microservices. It allows by default server side rendering for loading performance boost. You can connect it with other frameworks or languages because merkur defines easy API. You can use one of four predefined template's library [Preact](https://preactjs.com/), [µhtml](https://github.com/WebReflection/uhtml#readme), [Svelte](https://svelte.dev/) and [vanilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) but you can easily extend for others.
13+
14+
## Installation
15+
16+
We must import `loginPlugin` and register it to `$plugins` property of the widget. We also need to install the Session Storage plugin ([documentation for @merkur/plugin-session-storage](https://merkur.js.org/docs/session-storage-plugin)).
17+
18+
```javascript
19+
// ./src/widget.js
20+
import { loginPlugin } from '@merkur/plugin-login';
21+
22+
export const widgetProperties = {
23+
name,
24+
version,
25+
$plugins: [loginPlugin],
26+
// ... other properties
27+
};
28+
29+
// ./src/polyfill.js
30+
import 'whatwg-fetch';
31+
import 'abort-controller/polyfill';
32+
```
33+
34+
## Methods
35+
36+
### `listenToUserEvents`
37+
38+
- `userApiCall` - `function`
39+
- `[listener]` - `function` (optional)
40+
41+
The `listenToUserEvents` method returns an array of listeners bounded to user events (`badge`, `login`, `logout` and `forget`). This method binds the original listener to user events. If we omit listener parameter, it will use a default implementation, which will try to load a user data from `sessionStorage` cache or from an API using `userApiCall` function (which is a required parameter of this method). A loaded user object will be saved in the widget state under the `user` key with these properties:
42+
- `isLoggedIn` - `boolean`
43+
- `isVerified` - `boolean`
44+
- `firstname` - `string`
45+
- `lastname` - `string`
46+
47+
If you want to implement a custom event listener, it will receive these values as arguments in this specific order:
48+
- `widget` - `object`
49+
- `userApiCall` - `function`
50+
- `event` - `string` (an event name)
51+
- `eventData` - `*` (an event data)
52+
53+
```javascript
54+
// widget.userService.user() is our custom function which loads an user data from and API
55+
let listeners = widget.login.listenToUserEvents(widget.userService.user);
56+
```
57+
58+
### `unlistenUserEvents`
59+
- `listeners` - `function[]`
60+
61+
The `unlistenUserEvents` method unbinds listeners which where previously bounded using `listenToUserEvents` method.
62+
63+
```javascript
64+
let listeners = widget.login.listenToUserEvents(widget.userService.user);
65+
66+
widget.login.unlistenUserEvents(listeners)
67+
```
68+
69+
### `openLoginWindow`
70+
71+
The `openLoginWindow` opens a login window.
72+
73+
```javascript
74+
if (!widget.state.user.isLoggedIn) {
75+
widget.login.openLoginWindow();
76+
}
77+
```

Diff for: packages/plugin-login/babel.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: ['@babel/plugin-transform-modules-commonjs'],
3+
};

Diff for: packages/plugin-login/jest.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const defaultConfig = require('../../jest.config.js');
2+
3+
module.exports = { ...defaultConfig };

Diff for: packages/plugin-login/package.json

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "@merkur/plugin-login",
3+
"version": "0.0.9",
4+
"description": "Merkur Login.",
5+
"main": "lib/index",
6+
"module": "lib/index",
7+
"unpkg": "lib/index.umd.js",
8+
"sideEffects": false,
9+
"exports": {
10+
".": {
11+
"import": "./lib/index.mjs",
12+
"require": "./lib/index.cjs"
13+
},
14+
"./lib/index.es9.mjs": "./lib/index.es9.mjs",
15+
"./lib/index.es9.cjs": "./lib/index.es9.cjs"
16+
},
17+
"browser": {
18+
"./lib/index.js": "./lib/index.js",
19+
"./lib/index.cjs": "./lib/index.cjs",
20+
"./lib/index.mjs": "./lib/index.mjs",
21+
"./lib/index.es9.mjs": "./lib/index.es9.mjs"
22+
},
23+
"scripts": {
24+
"preversion": "npm test",
25+
"test": "jest --no-watchman -c ./jest.config.js",
26+
"test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
27+
"build": "rollup -c rollup.config.mjs",
28+
"prepare": "npm run build"
29+
},
30+
"repository": {
31+
"type": "git",
32+
"url": "git+https://github.com/mjancarik/merkur.git"
33+
},
34+
"keywords": [
35+
"merkur",
36+
"plugin",
37+
"microservices",
38+
"microfrontends"
39+
],
40+
"author": "Ondřej Baše",
41+
"license": "ISC",
42+
"bugs": {
43+
"url": "https://github.com/mjancarik/merkur/issues"
44+
},
45+
"publishConfig": {
46+
"registry": "https://registry.npmjs.org/",
47+
"access": "public"
48+
},
49+
"homepage": "https://merkur.js.org/",
50+
"devDependencies": {
51+
"@merkur/core": "^0.31.0"
52+
},
53+
"peerDependencies": {
54+
"@merkur/core": "*"
55+
},
56+
"dependencies": {}
57+
}

Diff for: packages/plugin-login/rollup.config.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {
2+
createRollupESConfig,
3+
createRollupES9Config,
4+
createRollupUMDConfig,
5+
} from '../../createRollupConfig.mjs';
6+
7+
let esConfig = createRollupESConfig();
8+
let es9Config = createRollupES9Config();
9+
let umdConfig = createRollupUMDConfig();
10+
11+
export default [esConfig, es9Config, umdConfig];

0 commit comments

Comments
 (0)