This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathplugins.js
113 lines (97 loc) · 2.97 KB
/
plugins.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//** RiotJS plugins that supposed to work the same way on client and server */
import client from 'client'
import {extend} from 'lodash'
import {COOKIE_NAME} from 'config/browser'
import {withRouter} from '@frontless/core/browser'
import Store from 'components/store'
const isBrowser = typeof window !== 'undefined'
const riot = isBrowser ? require('riot') : require('@frontless/riot')
// First register components
if (!isBrowser) {
const glob = require('glob')
const path = require('path')
const register = (file) => {
const tag = require(path.resolve(file))
const component = tag.default;
riot.register(component.name, component)
};
const test = (file) => {
return !file.startsWith('./specs/') && !file.startsWith('./node_modules/')
}
glob.sync( './**/*.riot' ).forEach( ( file ) => test(file) && register(file))
}
const Global = (instance) => {
instance.setGlobal = function(data) {
if (!isBrowser) {
const globals = JSON.parse(this.req.session.globals || '{}');
this.req.session.globals = JSON.stringify(extend(globals, data))
}
}.bind(instance)
Object.defineProperty(instance, 'globals', {
get: function() {
if (isBrowser) {
const el = document.getElementById('globals')
const data = el ? el.innerText : '{}'
return JSON.parse(data || '{}')
} else {
return JSON.parse(this.req.session.globals || '{}')
}
}.bind(instance)
})
if (isBrowser) {
const mounted = instance.onMounted;
instance.onMounted = function(props, state) {
mounted.bind(this)(props, state);
if (instance.onBrowser) {
instance.onBrowser.bind(instance)()
}
}.bind(instance)
}
}
const ClientPlugin = (instance) => {
Object.defineProperty(instance, 'client', {
get: function() {
return client.factory(this.req)
}.bind(instance)
})
instance.service = function(name) {
return this.client.service(name)
}.bind(instance);
};
const AuthPlugin = (instance) => {
const beforeRequest = instance.beforeRequest || (() => Promise.resolve());
instance.beforeRequest = function(props) {
if (!isBrowser) {
const {authenticated, user} = props.req.session;
const {loggedIn, group} = (instance.access || {});
if (loggedIn) {
if (!authenticated) {
return this.redirect('/login')
}
if (group && group !== user.group) {
return this.redirect('/login')
}
}
this.setGlobal({
authenticated: authenticated,
...user,
})
}
return Promise.resolve(beforeRequest.bind(instance)(props))
}.bind(instance)
instance.logout = function() {
if (isBrowser) {
const { CookieStorage } = require('cookie-storage')
new CookieStorage().removeItem(COOKIE_NAME)
this.redirect('/login')
}
}.bind(instance)
}
if (isBrowser) {
document.__GLOBAL = {}
}
riot.install(withRouter)
riot.install(Global)
riot.install(Store)
riot.install(ClientPlugin)
riot.install(AuthPlugin)