Blame view

client/routes/hooks.js 958 Bytes
3b214be5e   Ryan Glover   Convert all Coffe...
1
  /*
03f51985e   Ryan Glover   Rename client/rou...
2
3
  * Route Hooks
  * Hook functions for managing user access to routes.
3b214be5e   Ryan Glover   Convert all Coffe...
4
5
6
  */
  
  /*
03f51985e   Ryan Glover   Rename client/rou...
7
  * Define Hook Functions
3b214be5e   Ryan Glover   Convert all Coffe...
8
9
10
  */
  
  /*
03f51985e   Ryan Glover   Rename client/rou...
11
  * Hook: Check if a User is Logged In
3b214be5e   Ryan Glover   Convert all Coffe...
12
13
14
15
16
17
18
19
20
21
22
23
24
  * If a user is not logged in and attempts to go to an authenticated route,
  * re-route them to the login screen.
  */
  
  checkUserLoggedIn = function(){
    if( !Meteor.loggingIn() && !Meteor.user() ) {
      Router.go('/login');
    } else {
      this.next();
    }
  }
  
  /*
03f51985e   Ryan Glover   Rename client/rou...
25
  * Hook: Check if a User Exists
3b214be5e   Ryan Glover   Convert all Coffe...
26
  * If a user is logged in and attempts to go to a public route, re-route
03f51985e   Ryan Glover   Rename client/rou...
27
  * them to the index path.
3b214be5e   Ryan Glover   Convert all Coffe...
28
29
30
31
32
33
34
35
36
37
38
  */
  
  userAuthenticated = function(){
    if( !Meteor.loggingIn() && Meteor.user() ){
      Router.go('/');
    } else {
      this.next();
    }
  }
  
  /*
03f51985e   Ryan Glover   Rename client/rou...
39
  * Run Hooks
3b214be5e   Ryan Glover   Convert all Coffe...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  */
  
  Router.onBeforeAction(checkUserLoggedIn, {
    except: [
      'signup',
      'login',
      'recover-password',
      'reset-password'
    ]
  });
  
  Router.onBeforeAction(userAuthenticated, {
    only: [
      'signup',
      'login',
      'recover-password',
      'reset-password'
    ]
  });