-
Notifications
You must be signed in to change notification settings - Fork 2
/
triggers.js
executable file
·48 lines (42 loc) · 1.65 KB
/
triggers.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
import { check } from 'meteor/check'
import { Router } from './Router'
import { loggedIn, loggedOut } from '../utils/accountUtils'
/**
* Creates a trigger that redirects to a given route if the current user is not logged in.
* @param redirectRoute The route that is redirected to if the user is not logged in
* @return {loginTrigger} a function that can be added to the {triggersEnter} list
*/
export const createLoginTrigger = (redirectRoute) => {
check(redirectRoute.path, Function)
return function loginTrigger () {
if (loggedOut()) {
const location = Router.location()
const fullPath = redirectRoute.path(encodeURIComponent(location))
Router.go(fullPath)
}
}
}
/**
* Creates a trigger that redirects to a given route if the current user is logged in.
* @param redirectRoute The route that is redirected to if the user is logged in
* @return {loginTrigger} a function that can be added to the {triggersEnter} list
*/
export const createLoggedinTrigger = (redirectRoute) => {
check(redirectRoute.path, Function)
return function loggedTrigger () {
if (loggedIn()) {
const location = Router.location()
const fullPath = redirectRoute.path(encodeURIComponent(location))
Router.go(fullPath)
}
}
}
/**
* Creates a trigger that redirects to a given route if the current route is not found by given path
* @param redirectRoute The route that is redirected to if the current route is not found by given path
* @return {loginTrigger} a function that can be added to the {triggersEnter} list
*/
export const createNotFoundTrigger = (redirectRoute) => () => {
// log not found route
Router.go(redirectRoute)
}