MastoAPI and OAuth: allow login with either email or username.
authoreal <eal@waifu.club>
Wed, 18 Apr 2018 10:13:57 +0000 (13:13 +0300)
committereal <eal@waifu.club>
Wed, 18 Apr 2018 10:13:57 +0000 (13:13 +0300)
lib/pleroma/user.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/oauth/oauth_controller.ex
lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex
lib/pleroma/web/templates/o_auth/o_auth/show.html.eex

index e959fe67738bdc33f2d38238c4495d07a1f9e70d..c77fd6816af6a0cd1b4f04ecfaac7a4d518b4f9d 100644 (file)
@@ -250,6 +250,13 @@ defmodule Pleroma.User do
     Repo.get_by(User, nickname: nickname)
   end
 
+  def get_by_nickname_or_email(nickname_or_email) do
+    case user = Repo.get_by(User, nickname: nickname_or_email) do
+      %User{} -> user
+      nil -> Repo.get_by(User, email: nickname_or_email)
+    end
+  end
+
   def get_cached_user_info(user) do
     key = "user_info:#{user.id}"
     Cachex.get!(:user_cache, key, fallback: fn _ -> user_info(user) end)
index 21a3660c8714889ccbcd552dd5c3ab473c4d8f8a..10531ec15126b46c40959dd4e3d023b841e9b645 100644 (file)
@@ -693,7 +693,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   end
 
   def login_post(conn, %{"authorization" => %{"name" => name, "password" => password}}) do
-    with %User{} = user <- User.get_cached_by_nickname(name),
+    with %User{} = user <- User.get_by_nickname_or_email(name),
          true <- Pbkdf2.checkpw(password, user.password_hash),
          {:ok, app} <- get_or_make_app(),
          {:ok, auth} <- Authorization.create_authorization(app, user),
index 05f3666117bb7a3977d409adbb8bf4f7e2107eba..6297b7baeca9039e89335c5f21234808124dbcb5 100644 (file)
@@ -29,7 +29,7 @@ defmodule Pleroma.Web.OAuth.OAuthController do
             "redirect_uri" => redirect_uri
           } = params
       }) do
-    with %User{} = user <- User.get_cached_by_nickname(name),
+    with %User{} = user <- User.get_by_nickname_or_email(name),
          true <- Pbkdf2.checkpw(password, user.password_hash),
          %App{} = app <- Repo.get_by(App, client_id: client_id),
          {:ok, auth} <- Authorization.create_authorization(app, user) do
index 2ef67b901d0216ec5117751ef8d57872039c2b53..2bb54977edaf7279f819c1fa514d51ef14f09588 100644 (file)
@@ -3,7 +3,7 @@
   <h2><%= @error %></h2>
 <% end %>
 <%= form_for @conn, mastodon_api_path(@conn, :login), [as: "authorization"], fn f -> %>
-<%= text_input f, :name, placeholder: "Username" %>
+<%= text_input f, :name, placeholder: "Username or email" %>
 <br>
 <%= password_input f, :password, placeholder: "Password" %>
 <br>
index a7fa7523b80fc00a3feffe4777a4a472a6ea1869..de2241ec91dd283d38baf7d0d5db3b49d8c1d2ce 100644 (file)
@@ -2,7 +2,7 @@
 <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
 <h2>OAuth Authorization</h2>
 <%= form_for @conn, o_auth_path(@conn, :authorize), [as: "authorization"], fn f -> %>
-<%= label f, :name, "Name" %>
+<%= label f, :name, "Name or email" %>
 <%= text_input f, :name %>
 <br>
 <%= label f, :password, "Password" %>