Skip to content

Commit

Permalink
Redirect user to original url after auth, closes 68 (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbrazier committed Oct 1, 2019
1 parent 8b2842f commit ce9e791
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion api/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ func getRedirectUrl(c echo.Context) string {
return fmt.Sprintf("%s/callback", uri)
}

func getOriginalUrl(c echo.Context) string {
session, _ := store.Get(c.Request(), "session")

if redirectPath := session.Values["redirect"]; redirectPath != nil {
if path, ok := redirectPath.(string); ok {
return path
}
}

return "/"
}

// AuthInit initialize authentication
func (h *Handler) AuthInit(e *echo.Echo) {
appConfig := config.GetConfig()
Expand Down Expand Up @@ -68,6 +80,11 @@ func (h *Handler) auth(next echo.HandlerFunc) echo.HandlerFunc {
if session.Values["user"] != nil {
return next(c)
}

session.Values["redirect"] = c.Request().URL.Path
if err := sessions.Save(c.Request(), c.Response()); err != nil {
fmt.Printf("error saving redirect session: %v", err)
}
fmt.Println("Redirecting to auth")

endpointURL := fmt.Sprintf("https://login.microsoftonline.com/%s/oauth2/v2.0", appConfig.Auth.ADTenantID)
Expand Down Expand Up @@ -132,7 +149,7 @@ func (h *Handler) callbackHandler(c echo.Context) error {
if err := sessions.Save(c.Request(), c.Response()); err != nil {
return fmt.Errorf("error saving session: %v", err)
}
return c.Redirect(http.StatusTemporaryRedirect, "/")
return c.Redirect(http.StatusTemporaryRedirect, getOriginalUrl(c))
}

func getUserDetails(token string) (*User, error) {
Expand Down

0 comments on commit ce9e791

Please sign in to comment.