social-login: Make more graceful login
- Keep current page after logged in - Don't need to refresh after logged in
@df00f1f17ba7ce754d9a7282aa829bbe38970ae7
--- app/service/YonaUserServicePlugin.java
+++ app/service/YonaUserServicePlugin.java
... | ... | @@ -4,9 +4,12 @@ |
4 | 4 |
import com.feth.play.module.pa.user.AuthUser; |
5 | 5 |
import com.feth.play.module.pa.user.AuthUserIdentity; |
6 | 6 |
import com.feth.play.module.pa.user.BasicIdentity; |
7 |
+import controllers.UserApp; |
|
7 | 8 |
import models.User; |
8 | 9 |
import models.UserCredential; |
9 | 10 |
import play.Application; |
11 |
+ |
|
12 |
+import javax.annotation.Nonnull; |
|
10 | 13 |
|
11 | 14 |
public class YonaUserServicePlugin extends UserServicePlugin { |
12 | 15 |
private static boolean useSocialNameSync = play.Configuration.root().getBoolean("application.use.social.login.name.sync", false); |
... | ... | @@ -19,7 +22,14 @@ |
19 | 22 |
public Object save(final AuthUser authUser) { |
20 | 23 |
final boolean isLinked = UserCredential.existsByAuthUserIdentity(authUser); |
21 | 24 |
if (!isLinked) { |
22 |
- return UserCredential.create(authUser).id; |
|
25 |
+ UserCredential userCredential = UserCredential.create(authUser); |
|
26 |
+ User existed = User.findByEmail(userCredential.email); |
|
27 |
+ if (existed.isAnonymous()) { |
|
28 |
+ UserApp.createLocalUserWithOAuth(userCredential); |
|
29 |
+ } else { |
|
30 |
+ UserApp.addUserInfoToSession(existed); |
|
31 |
+ } |
|
32 |
+ return userCredential.id; |
|
23 | 33 |
} else { |
24 | 34 |
// we have this user already, so return null |
25 | 35 |
return null; |
... | ... | @@ -34,6 +44,7 @@ |
34 | 44 |
if(u != null) { |
35 | 45 |
if(useSocialNameSync && identity instanceof BasicIdentity){ |
36 | 46 |
BasicIdentity authUser = ((BasicIdentity) identity); |
47 |
+ setStatusLoggedIn(u, authUser); |
|
37 | 48 |
if(!u.name.equals(authUser.getName())){ |
38 | 49 |
updateLocalUserName(u, authUser); |
39 | 50 |
} |
... | ... | @@ -44,6 +55,20 @@ |
44 | 55 |
} |
45 | 56 |
} |
46 | 57 |
|
58 |
+ private void setStatusLoggedIn(@Nonnull UserCredential u, BasicIdentity authUser) { |
|
59 |
+ User localUser = User.findByEmail(authUser.getEmail()); |
|
60 |
+ if(localUser.isAnonymous() && u.loginId == null){ |
|
61 |
+ UserApp.createLocalUserWithOAuth(u); |
|
62 |
+ } else { |
|
63 |
+ if (u.loginId == null) { |
|
64 |
+ u.loginId = localUser.loginId; |
|
65 |
+ u.user = localUser; |
|
66 |
+ u.update(); |
|
67 |
+ } |
|
68 |
+ UserApp.addUserInfoToSession(localUser); |
|
69 |
+ } |
|
70 |
+ } |
|
71 |
+ |
|
47 | 72 |
private void updateLocalUserName(UserCredential u, BasicIdentity authUser) { |
48 | 73 |
u.name = authUser.getName(); |
49 | 74 |
u.update(); |
--- app/views/common/loginDialog.scala.html
+++ app/views/common/loginDialog.scala.html
... | ... | @@ -62,6 +62,8 @@ |
62 | 62 |
<a href="@p.getUrl" class="ybtn oauth-login-btn">@Html(providerWithLogo(p.getKey))</a> |
63 | 63 |
} |
64 | 64 |
} |
65 |
+ } else { |
|
66 |
+ <a href="@routes.Application.oAuthLogout()" class="ybtn oauth-login-btn">@Messages("title.logout")</a> |
|
65 | 67 |
} |
66 | 68 |
} |
67 | 69 |
</div> |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?