/
ConnectSnippet.scala
102 lines (82 loc) · 2.71 KB
/
ConnectSnippet.scala
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright 2010 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fbc.example {
package snippet {
import fbc.example.model._
import _root_.scala.xml.{NodeSeq, Text}
import _root_.net.liftweb.util._
import _root_.net.liftweb.common._
import net.liftweb.ext_api.facebook.{FacebookRestApi,FacebookClient, FacebookConnect, FacebookSession}
import net.liftweb.http.js.JsCmds
import net.liftweb.http.js.JE
import net.liftweb.http.js._
import net.liftweb.http.SHtml
import net.liftweb.http.S
import JsCmds._
import JE._
import Helpers._
class ConnectSnippet {
def initfb:NodeSeq = {
Script(Call("FB.init", Str(FacebookRestApi.apiKey), Str("/xd_receiver.html")))
}
def fbloginLink:NodeSeq = SHtml.a(Text("Login With Fb"),loginCmd)
def redirectRoot = RedirectTo("/")
def fbconnectLink(in:NodeSeq):NodeSeq = {
val user = User.currentUser.open_! //must be logged in
if (user.fbid.is == 0)
SHtml.a(Text("Link your account with fb"),connectCmd)
else
bind("f", in,
AttrBindParam("uid", Text(user.fbid.is.toString), "uid"),
"unlink" -> SHtml.a(() => {
user.fbid(0).save
Alert("Unlinked") & redirectRoot
},Text("Unlink from facebook"))
)
}
def requireSession(f: FacebookSession => JsCmd):JsCmd = {
val ajaxLogin = SHtml.ajaxInvoke(() => {
FacebookConnect.session match {
case Full(session) => f(session)
case _ => Alert("Failed to create Facebook session")
}
})._2
Call("FB.Connect.requireSession", AnonFunc(ajaxLogin))
}
def loginCmd:JsCmd = requireSession { session =>
User.findByFbId(session.uid) match {
case Full(user) =>
User.logUserIn(user)
S.notice("You're now logged in")
redirectRoot
case _ =>
S.error("You need to register first")
RedirectTo("/user_mgt/sign_up")
}
}
def connectCmd:JsCmd = requireSession { session =>
val user = User.currentUser.open_!
user.fbid(session.uid.toLong).validate match {
case Nil =>
user.save
S.notice("Linked to facebook connect")
case xs => S.error(xs)
}
redirectRoot
}
}
}
}