Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect user to original url after auth #70

Merged
merged 1 commit into from
Oct 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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