forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
81 lines (70 loc) · 2.02 KB
/
app.go
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
package controllers
import (
"code.google.com/p/goauth2/oauth"
"encoding/json"
"fmt"
"github.com/revel/revel"
"github.com/revel/revel/samples/facebook-oauth2/app/models"
"net/http"
"net/url"
"strconv"
)
type Application struct {
*revel.Controller
}
// The following keys correspond to a test application
// registered on Facebook, and associated with the loisant.org domain.
// You need to bind loisant.org to your machine with /etc/hosts to
// test the application locally.
var FACEBOOK = &oauth.Config{
ClientId: "95341411595",
ClientSecret: "8eff1b488da7fe3426f9ecaf8de1ba54",
AuthURL: "https://graph.facebook.com/oauth/authorize",
TokenURL: "https://graph.facebook.com/oauth/access_token",
RedirectURL: "http://loisant.org:9000/Application/Auth",
}
func (c Application) Index() revel.Result {
u := c.connected()
me := map[string]interface{}{}
if u != nil && u.AccessToken != "" {
resp, _ := http.Get("https://graph.facebook.com/me?access_token=" +
url.QueryEscape(u.AccessToken))
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&me); err != nil {
revel.ERROR.Println(err)
}
revel.INFO.Println(me)
}
authUrl := FACEBOOK.AuthCodeURL("foo")
return c.Render(me, authUrl)
}
func (c Application) Auth(code string) revel.Result {
t := &oauth.Transport{Config: FACEBOOK}
tok, err := t.Exchange(code)
if err != nil {
revel.ERROR.Println(err)
return c.Redirect(Application.Index)
}
user := c.connected()
user.AccessToken = tok.AccessToken
return c.Redirect(Application.Index)
}
func setuser(c *revel.Controller) revel.Result {
var user *models.User
if _, ok := c.Session["uid"]; ok {
uid, _ := strconv.ParseInt(c.Session["uid"], 10, 0)
user = models.GetUser(int(uid))
}
if user == nil {
user = models.NewUser()
c.Session["uid"] = fmt.Sprintf("%d", user.Uid)
}
c.RenderArgs["user"] = user
return nil
}
func init() {
revel.InterceptFunc(setuser, revel.BEFORE, &Application{})
}
func (c Application) connected() *models.User {
return c.RenderArgs["user"].(*models.User)
}