doortts doortts 2017-02-15
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
+++ app/service/YonaUserServicePlugin.java
@@ -4,9 +4,12 @@
 import com.feth.play.module.pa.user.AuthUser;
 import com.feth.play.module.pa.user.AuthUserIdentity;
 import com.feth.play.module.pa.user.BasicIdentity;
+import controllers.UserApp;
 import models.User;
 import models.UserCredential;
 import play.Application;
+
+import javax.annotation.Nonnull;
 
 public class YonaUserServicePlugin extends UserServicePlugin {
     private static boolean useSocialNameSync = play.Configuration.root().getBoolean("application.use.social.login.name.sync", false);
@@ -19,7 +22,14 @@
 	public Object save(final AuthUser authUser) {
 		final boolean isLinked = UserCredential.existsByAuthUserIdentity(authUser);
 		if (!isLinked) {
-			return UserCredential.create(authUser).id;
+			UserCredential userCredential = UserCredential.create(authUser);
+			User existed = User.findByEmail(userCredential.email);
+			if (existed.isAnonymous()) {
+				UserApp.createLocalUserWithOAuth(userCredential);
+			} else {
+				UserApp.addUserInfoToSession(existed);
+			}
+			return userCredential.id;
 		} else {
 			// we have this user already, so return null
 			return null;
@@ -34,6 +44,7 @@
 		if(u != null) {
 			if(useSocialNameSync && identity instanceof BasicIdentity){
 				BasicIdentity authUser = ((BasicIdentity) identity);
+				setStatusLoggedIn(u, authUser);
 				if(!u.name.equals(authUser.getName())){
 					updateLocalUserName(u, authUser);
 				}
@@ -44,6 +55,20 @@
 		}
 	}
 
+	private void setStatusLoggedIn(@Nonnull UserCredential u, BasicIdentity authUser) {
+		User localUser = User.findByEmail(authUser.getEmail());
+		if(localUser.isAnonymous() && u.loginId == null){
+            UserApp.createLocalUserWithOAuth(u);
+        } else {
+            if (u.loginId == null) {
+                u.loginId = localUser.loginId;
+                u.user = localUser;
+                u.update();
+            }
+            UserApp.addUserInfoToSession(localUser);
+        }
+	}
+
 	private void updateLocalUserName(UserCredential u, BasicIdentity authUser) {
 		u.name = authUser.getName();
 		u.update();
app/views/common/loginDialog.scala.html
--- app/views/common/loginDialog.scala.html
+++ app/views/common/loginDialog.scala.html
@@ -62,6 +62,8 @@
                 <a href="@p.getUrl" class="ybtn oauth-login-btn">@Html(providerWithLogo(p.getKey))</a>
               }
             }
+          } else {
+            <a href="@routes.Application.oAuthLogout()" class="ybtn oauth-login-btn">@Messages("title.logout")</a>
           }
         }
       </div>
app/views/common/usermenu.scala.html
--- app/views/common/usermenu.scala.html
+++ app/views/common/usermenu.scala.html
@@ -4,6 +4,7 @@
 * Copyright Yona & Yobi Authors & NAVER Corp.
 * https://yona.io
 **@
+@import org.apache.commons.lang3.StringUtils
 @(project:Project)
 @import utils.TemplateHelper._
 @import com.feth.play.module.pa.PlayAuthenticate._
@@ -13,13 +14,17 @@
 @if(UserApp.currentUser().isAnonymous){
     @currentAuth() { auth => @{
             if(auth == null) {
-                val url = storeOriginalUrl(Context.current())
-            } else {
-                UserApp.linkWithExistedOrCreateLocalUser()
+                val redirect = Context.current().request().getQueryString("redirectUrl")
+                if(StringUtils.isNotEmpty(redirect)) {
+                    val url = Context.current().session().put("pa.url.orig", redirect)
+                } else {
+                    val url = storeOriginalUrl(Context.current())
+                }
             }
         }
     }
 }
+
 <div id="mySidenav" class="sidenav">
     <div class="span5 right-menu span-hard-wrap">
         <div class="row-fluid user-menu-wrap">
Add a comment
List