Repository: pypi/warehouse

Issue:
Redirect from login page if user is logged in
Currently the ` accounts.login` view will still render even if there's already user logged in.

Since it doesn't make much sense for a user to log in twice, this view should check if there is a logged in user, and redirect to the post-login view if there is. (See #2882 for which view this should be).

---

**Good First Issue**: This issue is good for first time contributors. If there is not a corresponding pull request for this issue, it is up for grabs. For directions for getting set up, see our [Getting Started Guide](https://warehouse.pypa.io/development/getting-started/). If you are working on this issue and have questions, please feel free to ask them here, [`#pypa-dev` on Freenode](https://webchat.freenode.net/?channels=%23pypa-dev), or the [pypa-dev mailing list](https://groups.google.com/forum/#!forum/pypa-dev).


Discussion:
@waseem18 Would you like to take this on next?

(Until he says yes, this is still up for grabs.)
Absolutely Yes!

I'll start looking into it right away :+1: 
Cool, thanks, @waseem18 -- if you jump into IRC I have a few more suggestions for you.
Sure, Hopping in
This issue also exists for the view `accounts.register`

Resolving patch:
diff --git a/warehouse/accounts/views.py b/warehouse/accounts/views.py
--- a/warehouse/accounts/views.py
+++ b/warehouse/accounts/views.py
@@ -103,6 +103,8 @@ def login(request, redirect_field_name=REDIRECT_FIELD_NAME,
     # TODO: Logging in should reset request.user
     # TODO: Configure the login view as the default view for not having
     #       permission to view something.
+    if request.authenticated_userid is not None:
+        return HTTPSeeOther(request.route_path('manage.projects'))
 
     user_service = request.find_service(IUserService, context=None)
 
@@ -213,7 +215,7 @@ def logout(request, redirect_field_name=REDIRECT_FIELD_NAME):
 )
 def register(request, _form_class=RegistrationForm):
     if request.authenticated_userid is not None:
-        return HTTPSeeOther("/")
+        return HTTPSeeOther(request.route_path('manage.projects'))
 
     if AdminFlag.is_enabled(request.db, 'disallow-new-user-registration'):
         request.session.flash(


Test patch:
diff --git a/tests/unit/accounts/test_views.py b/tests/unit/accounts/test_views.py
--- a/tests/unit/accounts/test_views.py
+++ b/tests/unit/accounts/test_views.py
@@ -238,6 +238,15 @@ def test_post_validate_no_redirects(self, pyramid_request,
         assert isinstance(result, HTTPSeeOther)
         assert result.headers["Location"] == observed_next_url
 
+    def test_redirect_authenticated_user(self):
+        pyramid_request = pretend.stub(authenticated_userid=1)
+        pyramid_request.route_path = pretend.call_recorder(
+            lambda a: '/the-redirect'
+        )
+        result = views.login(pyramid_request)
+        assert isinstance(result, HTTPSeeOther)
+        assert result.headers["Location"] == "/the-redirect"
+
 
 class TestLogout:
 
@@ -303,9 +312,13 @@ def test_get(self, db_request):
         assert result["form"] is form_inst
 
     def test_redirect_authenticated_user(self):
-        result = views.register(pretend.stub(authenticated_userid=1))
+        pyramid_request = pretend.stub(authenticated_userid=1)
+        pyramid_request.route_path = pretend.call_recorder(
+            lambda a: '/the-redirect'
+        )
+        result = views.register(pyramid_request)
         assert isinstance(result, HTTPSeeOther)
-        assert result.headers["Location"] == "/"
+        assert result.headers["Location"] == "/the-redirect"
 
     def test_register_redirect(self, db_request, monkeypatch):
         db_request.method = "POST"