1
- const BEBOP_LOCAL_STORAGE_TOKEN_KEY = ' bebop_auth_token'
2
- const BEBOP_OAUTH_RESULT_COOKIE = ' bebop_oauth_result'
1
+ const BEBOP_LOCAL_STORAGE_TOKEN_KEY = " bebop_auth_token" ;
2
+ const BEBOP_OAUTH_RESULT_COOKIE = " bebop_oauth_result" ;
3
3
4
4
var BebopApp = new Vue ( {
5
- el : '#app' ,
6
-
7
- template : `
8
- <div>
9
- <bebop-nav :config="config" :auth="auth"></bebop-nav>
10
- <bebop-username-modal ref="usernameModal"></bebop-username-modal>
11
- <router-view :config="config" :auth="auth"></router-view>
12
- </div>
13
- ` ,
14
-
15
- router : new VueRouter ( {
16
- routes : [
17
- { path : '/' , component : BebopTopics } ,
18
- { path : '/p/:page' , component : BebopTopics } ,
19
- { path : '/t/:topic' , component : BebopComments } ,
20
- { path : '/t/:topic/p/:page' , component : BebopComments } ,
21
- { path : '/t/:topic/p/:page/c/:comment' , component : BebopComments } ,
22
- { path : '/new-topic' , component : BebopNewTopic } ,
23
- { path : '/new-comment/:topic' , component : BebopNewComment } ,
24
- { path : '/me' , component : BebopUser } ,
25
- { path : '/u/:user' , component : BebopUser } ,
26
- ] ,
27
- scrollBehavior : function ( to , from , savedPosition ) {
28
- if ( savedPosition ) {
29
- return savedPosition
30
- } else {
31
- return { x : 0 , y : 0 }
32
- }
33
- }
34
- } ) ,
35
-
36
- data : function ( ) {
37
- return {
38
- config : appConfig ,
39
- auth : {
40
- authenticated : false ,
41
- user : { }
42
- } ,
43
- }
44
- } ,
45
-
46
- mounted : function ( ) {
47
- this . checkAuth ( )
48
- } ,
49
-
50
- methods : {
51
- signIn : function ( provider ) {
52
- window . open ( "oauth/begin/" + provider , "" , "width=800,height=600" ) ;
53
- } ,
54
-
55
- signOut : function ( ) {
56
- localStorage . removeItem ( BEBOP_LOCAL_STORAGE_TOKEN_KEY )
57
- Vue . http . headers . common [ 'Authorization' ] = ""
58
- this . auth = {
59
- authenticated : false ,
60
- user : { }
61
- }
62
- } ,
63
-
64
- oauthEnd : function ( ) {
65
- var result = this . getCookieByName ( BEBOP_OAUTH_RESULT_COOKIE )
66
- var parts = result . split ( ":" )
67
-
68
- if ( parts . length !== 2 ) {
69
- this . oauthError ( "Unknown" )
70
- return
71
- }
72
-
73
- if ( parts [ 0 ] === "error" ) {
74
- this . oauthError ( parts [ 1 ] )
75
- return
76
- }
77
-
78
- if ( parts [ 0 ] !== "success" ) {
79
- this . oauthError ( "Unknown" )
80
- return
81
- }
82
-
83
- this . oauthSuccess ( parts [ 1 ] )
84
- } ,
85
-
86
- getCookieByName : function ( name ) {
87
- var value = "; " + document . cookie
88
- var parts = value . split ( "; " + name + "=" )
89
- if ( parts . length == 2 ) return parts . pop ( ) . split ( ";" ) . shift ( )
90
- } ,
91
-
92
- oauthSuccess : function ( token ) {
93
- localStorage . setItem ( BEBOP_LOCAL_STORAGE_TOKEN_KEY , token )
94
- this . checkAuth ( )
95
- } ,
96
-
97
- oauthError : function ( error ) {
98
- if ( error === "UserBlocked" ) {
99
- console . log ( "oauth error: USER IS BLOCKED" )
100
- } else {
101
- console . log ( "oauth error: " + error )
102
- }
103
- this . signOut ( )
104
- } ,
105
-
106
- checkAuth : function ( ) {
107
- var token = localStorage . getItem ( BEBOP_LOCAL_STORAGE_TOKEN_KEY )
108
- if ( token ) {
109
- Vue . http . headers . common [ 'Authorization' ] = 'Bearer ' + token
110
- }
111
- this . getMe ( )
112
- } ,
113
-
114
- getMe : function ( ) {
115
- this . $http . get ( 'api/v1/me' ) . then (
116
- response => {
117
- this . auth = {
118
- authenticated : response . body . authenticated ? true : false ,
119
- user : response . body . authenticated ? response . body . user : { } ,
120
- }
121
- if ( this . auth . authenticated && this . auth . user . name == "" ) {
122
- this . setMyName ( )
123
- }
124
- } ,
125
- response => {
126
- console . log ( "ERROR: getMe: " + JSON . stringify ( response . body ) )
127
- if ( response . status == 401 ) {
128
- this . signOut ( )
129
- }
130
- }
131
- )
132
- } ,
133
-
134
- setMyName : function ( ) {
135
- this . $refs . usernameModal . show (
136
- this . auth . user . id ,
137
- "" ,
138
- ( success ) => {
139
- if ( ! success ) {
140
- this . signOut ( )
141
- }
142
- this . getMe ( )
143
- }
144
- )
145
- }
146
- }
147
-
148
- } )
5
+ el : "#app" ,
6
+
7
+ template : `
8
+ <div>
9
+ <bebop-nav :config="config" :auth="auth"></bebop-nav>
10
+ <bebop-username-modal ref="usernameModal"></bebop-username-modal>
11
+ <router-view :config="config" :auth="auth"></router-view>
12
+ </div>
13
+ ` ,
14
+
15
+ router : new VueRouter ( {
16
+ routes : [
17
+ { path : "/" , component : BebopTopics } ,
18
+ { path : "/p/:page" , component : BebopTopics } ,
19
+ { path : "/t/:topic" , component : BebopComments } ,
20
+ { path : "/t/:topic/p/:page" , component : BebopComments } ,
21
+ { path : "/t/:topic/p/:page/c/:comment" , component : BebopComments } ,
22
+ { path : "/new-topic" , component : BebopNewTopic } ,
23
+ { path : "/new-comment/:topic" , component : BebopNewComment } ,
24
+ { path : "/me" , component : BebopUser } ,
25
+ { path : "/u/:user" , component : BebopUser } ,
26
+ ] ,
27
+ scrollBehavior : function ( to , from , savedPosition ) {
28
+ if ( savedPosition ) {
29
+ return savedPosition ;
30
+ } else {
31
+ return { x : 0 , y : 0 } ;
32
+ }
33
+ } ,
34
+ } ) ,
35
+
36
+ data : function ( ) {
37
+ return {
38
+ config : appConfig ,
39
+ auth : {
40
+ authenticated : false ,
41
+ user : { } ,
42
+ } ,
43
+ } ;
44
+ } ,
45
+
46
+ mounted : function ( ) {
47
+ this . checkAuth ( ) ;
48
+ } ,
49
+
50
+ methods : {
51
+ signIn : function ( provider ) {
52
+ window . open ( "oauth/begin/" + provider , "" , "width=800,height=600" ) ;
53
+ } ,
54
+
55
+ signOut : function ( ) {
56
+ localStorage . removeItem ( BEBOP_LOCAL_STORAGE_TOKEN_KEY ) ;
57
+ Vue . http . headers . common [ "Authorization" ] = "" ;
58
+ this . auth = {
59
+ authenticated : false ,
60
+ user : { } ,
61
+ } ;
62
+ } ,
63
+
64
+ oauthEnd : function ( ) {
65
+ var result = this . getCookieByName ( BEBOP_OAUTH_RESULT_COOKIE ) ;
66
+ var parts = result . split ( ":" ) ;
67
+
68
+ if ( parts . length !== 2 ) {
69
+ this . oauthError ( "Unknown" ) ;
70
+ return ;
71
+ }
72
+
73
+ if ( parts [ 0 ] === "error" ) {
74
+ this . oauthError ( parts [ 1 ] ) ;
75
+ return ;
76
+ }
77
+
78
+ if ( parts [ 0 ] !== "success" ) {
79
+ this . oauthError ( "Unknown" ) ;
80
+ return ;
81
+ }
82
+
83
+ this . oauthSuccess ( parts [ 1 ] ) ;
84
+ } ,
85
+
86
+ getCookieByName : function ( name ) {
87
+ var value = "; " + document . cookie ;
88
+ var parts = value . split ( "; " + name + "=" ) ;
89
+ if ( parts . length === 2 ) return parts . pop ( ) . split ( ";" ) . shift ( ) ;
90
+ } ,
91
+
92
+ oauthSuccess : function ( token ) {
93
+ localStorage . setItem ( BEBOP_LOCAL_STORAGE_TOKEN_KEY , token ) ;
94
+ this . checkAuth ( ) ;
95
+ } ,
96
+
97
+ oauthError : function ( error ) {
98
+ if ( error === "UserBlocked" ) {
99
+ console . log ( "oauth error: USER IS BLOCKED" ) ;
100
+ } else {
101
+ console . log ( "oauth error: " + error ) ;
102
+ }
103
+ this . signOut ( ) ;
104
+ } ,
105
+
106
+ checkAuth : function ( ) {
107
+ var token = localStorage . getItem ( BEBOP_LOCAL_STORAGE_TOKEN_KEY ) ;
108
+ if ( token ) {
109
+ Vue . http . headers . common [ "Authorization" ] = "Bearer " + token ;
110
+ }
111
+ this . getMe ( ) ;
112
+ } ,
113
+
114
+ getMe : function ( ) {
115
+ this . $http . get ( "api/v1/me" ) . then (
116
+ response => {
117
+ this . auth = {
118
+ authenticated : response . body . authenticated ? true : false ,
119
+ user : response . body . authenticated ? response . body . user : { } ,
120
+ } ;
121
+ if ( this . auth . authenticated && this . auth . user . name === "" ) {
122
+ this . setMyName ( ) ;
123
+ }
124
+ } ,
125
+ response => {
126
+ console . log ( "ERROR: getMe: " + JSON . stringify ( response . body ) ) ;
127
+ if ( response . status === 401 ) {
128
+ this . signOut ( ) ;
129
+ }
130
+ }
131
+ ) ;
132
+ } ,
133
+
134
+ setMyName : function ( ) {
135
+ this . $refs . usernameModal . show ( this . auth . user . id , "" , success => {
136
+ if ( ! success ) {
137
+ this . signOut ( ) ;
138
+ }
139
+ this . getMe ( ) ;
140
+ } ) ;
141
+ } ,
142
+ } ,
143
+ } ) ;
149
144
150
145
function bebopOAuthEnd ( ) {
151
- BebopApp . oauthEnd ( )
152
- }
146
+ BebopApp . oauthEnd ( ) ;
147
+ }
0 commit comments