diff --git a/handlers/auth.go b/handlers/auth.go
index dd0f29d887..3c93d4c4e6 100644
--- a/handlers/auth.go
+++ b/handlers/auth.go
@@ -168,6 +168,20 @@ func Login(w http.ResponseWriter, r *http.Request) {
 		RecaptchaKey: utils.Config.Frontend.RecaptchaSiteKey,
 	}
 
+	if redirect := q.Get("redirect"); redirect != "" {
+		http.SetCookie(w, &http.Cookie{
+			Name:   "redirect-after",
+			Value:  redirect,
+			MaxAge: 300,
+		})
+	} else if redirect := q.Get("redirect_uri"); redirect != "" {
+		http.SetCookie(w, &http.Cookie{
+			Name:   "redirect-after",
+			Value:  redirect + "&state=" + q.Get("state"),
+			MaxAge: 300,
+		})
+	}
+
 	redirectData := struct {
 		Redirect_uri string
 		State        string
@@ -339,6 +353,12 @@ func LoginPost(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	cookie, err := r.Cookie("redirect-after")
+	if err == nil && cookie.Value != "" {
+		http.Redirect(w, r, cookie.Value, http.StatusSeeOther)
+		return
+	}
+
 	// Index(w, r)
 	http.Redirect(w, r, "/user/notifications", http.StatusSeeOther)
 }
diff --git a/handlers/user.go b/handlers/user.go
index 25ebb1587f..7d6c09f00e 100644
--- a/handlers/user.go
+++ b/handlers/user.go
@@ -39,7 +39,7 @@ func UserAuthMiddleware(next http.Handler) http.Handler {
 		user := getUser(r)
 		if !user.Authenticated {
 			utils.SetFlash(w, r, authSessionName, "Error: Please login first")
-			http.Redirect(w, r, "/login", http.StatusSeeOther)
+			http.Redirect(w, r, "/login?redirect="+r.URL.Path, http.StatusSeeOther)
 			return
 		}
 		next.ServeHTTP(w, r)