From f7a66147489214a996541eddc93eca0f26e83902 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 8 Mar 2016 17:21:07 +0000 Subject: [PATCH] Refactoring --- PlexRequests.UI/Modules/UserLoginModule.cs | 28 ++++++++++------------ 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/PlexRequests.UI/Modules/UserLoginModule.cs b/PlexRequests.UI/Modules/UserLoginModule.cs index 48cfdf8ff..cdc659d47 100644 --- a/PlexRequests.UI/Modules/UserLoginModule.cs +++ b/PlexRequests.UI/Modules/UserLoginModule.cs @@ -37,10 +37,7 @@ using PlexRequests.UI.Models; namespace PlexRequests.UI.Modules { - // TODO: Check the settings to see if we need to authenticate // TODO: Add ability to logout - // TODO: If we need to authenticate we need to check if they are in Plex - // TODO: Allow the user of a username only or a Username and password public class UserLoginModule : NancyModule { public UserLoginModule(ISettingsService auth) : base("userlogin") @@ -72,36 +69,35 @@ namespace PlexRequests.UI.Modules if (settings.UserAuthentication && settings.UsePassword) // Authenticate with Plex { - var signedIn = (PlexAuthentication)api.SignIn(username, password); if (signedIn.user?.authentication_token != null) { - var users = api.GetUsers(settings.PlexAuthToken); - if (users.User.Any(x => x.Username == username)) - { - authenticated = true; - } + authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken); } } else if(settings.UserAuthentication) // Check against the users in Plex { - var users = api.GetUsers(settings.PlexAuthToken); - if (users.User.Any(x => x.Username == username)) - { - authenticated = true; - } + authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken); } - else if(!settings.UserAuthentication) + else if(!settings.UserAuthentication) // No auth, let them pass! { authenticated = true; } - // Add to the session + // Add to the session (Used in the BaseModules) Session[SessionKeys.UsernameKey] = (string)username; + return Response.AsJson(authenticated ? new JsonResponseModel { Result = true } : new JsonResponseModel { Result = false, Message = "Incorrect User or Password"}); } + + private bool CheckIfUserIsInPlexFriends(string username, string authToken) + { + var api = new PlexApi(); + var users = api.GetUsers(authToken); + return users.User.Any(x => x.Username == username); + } } } \ No newline at end of file