@@ -61,77 +61,76 @@ github.com/bradrydzewski/go.auth
61
61
1 . 添加两条路由
62
62
``` Go
63
63
64
- beego.RegisterController (" /auth/login" , &controllers.GithubController {})
65
- beego.RegisterController (" /mainpage" , &controllers.PageController {})
64
+ beego.RegisterController (" /auth/login" , &controllers.GithubController {})
65
+ beego.RegisterController (" /mainpage" , &controllers.PageController {})
66
66
```
67
67
2 . 然后我们处理GithubController登陆的页面:
68
68
``` Go
69
+ package controllers
69
70
70
- package controllers
71
-
72
- import (
73
- " github.com/astaxie/beego"
74
- " github.com/bradrydzewski/go.auth"
75
- )
76
-
77
- const (
78
- githubClientKey = " a0864ea791ce7e7bd0df"
79
- githubSecretKey = " a0ec09a647a688a64a28f6190b5a0d2705df56ca"
80
- )
81
-
82
- type GithubController struct {
83
- beego.Controller
84
- }
85
-
86
- func (this *GithubController ) Get () {
87
- // set the auth parameters
88
- auth.Config .CookieSecret = []byte (" 7H9xiimk2QdTdYI7rDddfJeV" )
89
- auth.Config .LoginSuccessRedirect = " /mainpage"
90
- auth.Config .CookieSecure = false
91
-
92
- githubHandler := auth.Github (githubClientKey, githubSecretKey)
93
-
94
- githubHandler.ServeHTTP (this.Ctx .ResponseWriter , this.Ctx .Request )
95
- }
71
+ import (
72
+ " github.com/astaxie/beego"
73
+ " github.com/bradrydzewski/go.auth"
74
+ )
75
+
76
+ const (
77
+ githubClientKey = " a0864ea791ce7e7bd0df"
78
+ githubSecretKey = " a0ec09a647a688a64a28f6190b5a0d2705df56ca"
79
+ )
80
+
81
+ type GithubController struct {
82
+ beego.Controller
83
+ }
84
+
85
+ func (this *GithubController ) Get () {
86
+ // set the auth parameters
87
+ auth.Config .CookieSecret = []byte (" 7H9xiimk2QdTdYI7rDddfJeV" )
88
+ auth.Config .LoginSuccessRedirect = " /mainpage"
89
+ auth.Config .CookieSecure = false
90
+
91
+ githubHandler := auth.Github (githubClientKey, githubSecretKey)
92
+
93
+ githubHandler.ServeHTTP (this.Ctx .ResponseWriter , this.Ctx .Request )
94
+ }
96
95
97
96
```
98
97
3 . 处理登陆成功之后的页面
99
98
``` Go
99
+ package controllers
100
100
101
- package controllers
102
-
103
- import (
104
- " github.com/astaxie/beego"
105
- " github.com/bradrydzewski/go.auth"
106
- " net/http"
107
- " net/url"
108
- )
109
-
110
- type PageController struct {
111
- beego.Controller
112
- }
113
-
114
- func (this *PageController ) Get () {
115
- // set the auth parameters
116
- auth.Config .CookieSecret = []byte (" 7H9xiimk2QdTdYI7rDddfJeV" )
117
- auth.Config .LoginSuccessRedirect = " /mainpage"
118
- auth.Config .CookieSecure = false
119
-
120
- user , err := auth.GetUserCookie (this.Ctx .Request )
121
-
122
- // if no active user session then authorize user
123
- if err != nil || user.Id () == " " {
124
- http.Redirect (this.Ctx .ResponseWriter , this.Ctx .Request , auth.Config .LoginRedirect , http.StatusSeeOther )
125
- return
126
- }
127
-
128
- // else, add the user to the URL and continue
129
- this.Ctx .Request .URL .User = url.User (user.Id ())
130
- this.Data [" pic" ] = user.Picture ()
131
- this.Data [" id" ] = user.Id ()
132
- this.Data [" name" ] = user.Name ()
133
- this.TplNames = " home.tpl"
101
+ import (
102
+ " github.com/astaxie/beego"
103
+ " github.com/bradrydzewski/go.auth"
104
+ " net/http"
105
+ " net/url"
106
+ )
107
+
108
+ type PageController struct {
109
+ beego.Controller
110
+ }
111
+
112
+ func (this *PageController ) Get () {
113
+ // set the auth parameters
114
+ auth.Config .CookieSecret = []byte (" 7H9xiimk2QdTdYI7rDddfJeV" )
115
+ auth.Config .LoginSuccessRedirect = " /mainpage"
116
+ auth.Config .CookieSecure = false
117
+
118
+ user , err := auth.GetUserCookie (this.Ctx .Request )
119
+
120
+ // if no active user session then authorize user
121
+ if err != nil || user.Id () == " " {
122
+ http.Redirect (this.Ctx .ResponseWriter , this.Ctx .Request , auth.Config .LoginRedirect , http.StatusSeeOther )
123
+ return
134
124
}
125
+
126
+ // else, add the user to the URL and continue
127
+ this.Ctx .Request .URL .User = url.User (user.Id ())
128
+ this.Data [" pic" ] = user.Picture ()
129
+ this.Data [" id" ] = user.Id ()
130
+ this.Data [" name" ] = user.Name ()
131
+ this.TplNames = " home.tpl"
132
+ }
133
+
135
134
```
136
135
整个的流程如下,首先打开浏览器输入地址:
137
136
0 commit comments