Просмотр исходного кода

support invite after login/register

Alexander Belanger 5 лет назад
Родитель
Сommit
0b4acf26a8

+ 11 - 1
server/api/invite_handler_test.go

@@ -173,7 +173,17 @@ var acceptInviteTests = []*inviteTest{
 
 				projects, err := tester.repo.Project.ListProjectsByUserID(user.ID)
 
-				t.Errorf("%v\n", projects)
+				if len(projects) != 1 {
+					t.Fatalf("length of projects not 1\n")
+				}
+
+				if projects[0].ID != 1 {
+					t.Fatalf("project id was not 1\n")
+				}
+
+				if projects[0].Name != "project-test" {
+					t.Fatalf("project was not project-test\n")
+				}
 			},
 		},
 	},

+ 22 - 10
server/api/user_handler.go

@@ -50,12 +50,18 @@ func (app *App) HandleCreateUser(w http.ResponseWriter, r *http.Request) {
 		session.Values["email"] = user.Email
 		session.Save(r, w)
 
-		w.WriteHeader(http.StatusCreated)
-
-		if err := app.sendUser(w, user.ID, user.Email); err != nil {
-			app.handleErrorFormDecoding(err, ErrUserDecode, w)
-			return
+		if val, ok := session.Values["redirect"].(string); ok && val != "" {
+			http.Redirect(w, r, val, 302)
+		} else {
+			http.Redirect(w, r, "/dashboard", 302)
 		}
+
+		// w.WriteHeader(http.StatusCreated)
+
+		// if err := app.sendUser(w, user.ID, user.Email); err != nil {
+		// 	app.handleErrorFormDecoding(err, ErrUserDecode, w)
+		// 	return
+		// }
 	}
 }
 
@@ -122,12 +128,18 @@ func (app *App) HandleLoginUser(w http.ResponseWriter, r *http.Request) {
 		app.Logger.Warn().Err(err)
 	}
 
-	w.WriteHeader(http.StatusOK)
-
-	if err := app.sendUser(w, storedUser.ID, storedUser.Email); err != nil {
-		app.handleErrorFormDecoding(err, ErrUserDecode, w)
-		return
+	if val, ok := session.Values["redirect"].(string); ok && val != "" {
+		http.Redirect(w, r, val, 302)
+	} else {
+		http.Redirect(w, r, "/dashboard", 302)
 	}
+
+	// w.WriteHeader(http.StatusOK)
+
+	// if err := app.sendUser(w, storedUser.ID, storedUser.Email); err != nil {
+	// 	app.handleErrorFormDecoding(err, ErrUserDecode, w)
+	// 	return
+	// }
 }
 
 // HandleLogoutUser detaches the user from the session

+ 25 - 0
server/router/middleware/auth.go

@@ -46,6 +46,31 @@ func (auth *Auth) BasicAuthenticate(next http.Handler) http.Handler {
 	})
 }
 
+// BasicAuthenticateWithRedirect checks that a user is logged in, and if they're not, the
+// user is redirected to the login page with the redirect path stored in the session
+func (auth *Auth) BasicAuthenticateWithRedirect(next http.Handler) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if auth.isLoggedIn(w, r) {
+			next.ServeHTTP(w, r)
+		} else {
+			session, err := auth.store.Get(r, auth.cookieName)
+
+			if err != nil {
+				http.Redirect(w, r, "/dashboard", 302)
+			}
+
+			// need state parameter to validate when redirected
+			session.Values["redirect"] = r.URL.Path
+			session.Save(r, w)
+
+			http.Redirect(w, r, "/dashboard", 302)
+			return
+		}
+
+		return
+	})
+}
+
 // IDLocation represents the location of the ID to use for authentication
 type IDLocation uint
 

+ 1 - 1
server/router/router.go

@@ -217,7 +217,7 @@ func New(a *api.App) *chi.Mux {
 		r.Method(
 			"GET",
 			"/projects/{project_id}/invites/{token}",
-			auth.BasicAuthenticate(
+			auth.BasicAuthenticateWithRedirect(
 				requestlog.NewHandler(a.HandleAcceptInvite, l),
 			),
 		)