2
0
Эх сурвалжийг харах

[hotfix] Redirect to `/register` for invite accept for unauth requests (#2918)

* in case of invite accept, redirect to register with email for unauth requests

* hide github and login switch when receiving invite and logged out

---------

Co-authored-by: Porter Support <support@porter.run>
Co-authored-by: Justin Rhee <jusrhee@sas.upenn.edu>
Mohammed Nafees 3 жил өмнө
parent
commit
b9118fc5a2

+ 24 - 3
api/server/authn/handler.go

@@ -112,12 +112,33 @@ func (authn *AuthN) handleForbiddenForSession(
 
 		session.Save(r, w)
 
-		http.Redirect(w, r, "/dashboard", 302)
+		// special logic for GET /api/projects/{project_id}/invites/{token}
+		if r.Method == "GET" && strings.Contains(r.URL.Path, "/invites/") &&
+			!strings.HasSuffix(r.URL.Path, "/invites/") {
+			pathSegments := strings.Split(r.URL.Path, "/")
+			inviteToken := pathSegments[len(pathSegments)-1]
+
+			invite, err := authn.config.Repo.Invite().ReadInviteByToken(inviteToken)
+			if err != nil || invite.ProjectID == 0 || invite.Email == "" {
+				apierrors.HandleAPIError(authn.config.Logger, authn.config.Alerter, w, r,
+					apierrors.NewErrPassThroughToClient(fmt.Errorf("invalid invite token"), http.StatusBadRequest), true)
+				return
+			}
+
+			if invite.IsExpired() || invite.IsAccepted() {
+				apierrors.HandleAPIError(authn.config.Logger, authn.config.Alerter, w, r,
+					apierrors.NewErrPassThroughToClient(fmt.Errorf("invite has expired"), http.StatusBadRequest), true)
+				return
+			}
+
+			http.Redirect(w, r, "/register?email="+invite.Email, http.StatusTemporaryRedirect)
+			return
+		}
+
+		http.Redirect(w, r, "/dashboard", http.StatusFound)
 	} else {
 		authn.sendForbiddenError(err, w, r)
 	}
-
-	return
 }
 
 func (authn *AuthN) verifyTokenWithNext(w http.ResponseWriter, r *http.Request, tok *token.Token) {

+ 7 - 3
dashboard/src/components/porter/Input.tsx

@@ -91,6 +91,7 @@ const StyledInput = styled.input<{
   width: string;
   height: string;
   hasError: boolean;
+  disabled: boolean;
 }>`
   height: ${props => props.height || "35px"};
   padding: 5px 10px;
@@ -100,9 +101,12 @@ const StyledInput = styled.input<{
   outline: none;
   border-radius: 5px;
   background: #26292e;
+  cursor: ${props => props.disabled ? "not-allowed" : ""};
 
   border: 1px solid ${props => props.hasError ? "#ff3b62" : "#494b4f"};
-  :hover {
-    border: 1px solid ${props => props.hasError ? "#ff3b62" : "#7a7b80"};
-  }
+  ${props => !props.disabled && `
+    :hover {
+      border: 1px solid ${props.hasError ? "#ff3b62" : "#7a7b80"};
+    }
+  `}
 `;

+ 12 - 8
dashboard/src/main/auth/Register.tsx

@@ -194,7 +194,7 @@ const Register: React.FC<Props> = ({
           Create your Porter account
         </Heading>
         <Spacer y={1} />
-        {(hasGithub || hasGoogle) && (
+        {((hasGithub || hasGoogle) && !disabled) && (
           <>
             <Container row>
               {hasGithub && (
@@ -305,13 +305,17 @@ const Register: React.FC<Props> = ({
             </Button>
           </>
         )}
-        <Spacer y={1} />
-        <Text 
-          size={13}
-          color="helper"
-        >
-          Already have an account?<Spacer width="5px" inline /><Link to="/login">Log in</Link>
-        </Text>
+        {!disabled && (
+          <>
+            <Spacer y={1} />
+            <Text 
+              size={13}
+              color="helper"
+            >
+              Already have an account?<Spacer width="5px" inline /><Link to="/login">Log in</Link>
+            </Text>
+          </>
+        )}
       </Wrapper>
     </StyledRegister>
   );