Skip to content

Commit

Permalink
Add Login util method
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Aug 4, 2017
1 parent 1d090d3 commit 33cb82f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
15 changes: 14 additions & 1 deletion claims/claims.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package claims

import jwt "github.com/dgrijalva/jwt-go"
import (
jwt "github.com/dgrijalva/jwt-go"
)

// Claims auth claims
type Claims struct {
Provider string `json:"provider,omitempty"`
UserID string `json:"userid,omitempty"`
jwt.StandardClaims
}

// ToClaims implement ClaimerInterface
func (claims *Claims) ToClaims() *Claims {
return claims
}

// ClaimerInterface claimer interface
type ClaimerInterface interface {
ToClaims() *Claims
}
6 changes: 2 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
)

func respondAfterLogged(claims *claims.Claims, context *Context) {
token := context.Auth.SignedToken(claims)

// Set auth session
context.SessionManager.Add(context.Request, context.Auth.Config.SessionName, token)
// login user
context.Auth.Login(claims, context.Request)

responder.With("html", func() {
// write cookie
Expand Down
10 changes: 9 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"net/http"

"github.com/qor/auth/claims"
"github.com/qor/qor/utils"
)

Expand Down Expand Up @@ -33,8 +34,15 @@ func (auth *Auth) GetCurrentUser(req *http.Request) interface{} {
return nil
}

// Logout log current user out
// Logout sign current user out
func (auth *Auth) Logout(w http.ResponseWriter, req *http.Request) {
context := &Context{Auth: auth, Request: req, Writer: w}
auth.LogoutHandler(context)
}

// Login sign user in
func (auth *Auth) Login(claimer claims.ClaimerInterface, req *http.Request) error {
claims := claimer.ToClaims()
token := auth.SignedToken(claims)
return auth.SessionManager.Add(req, auth.Config.SessionName, token)
}

0 comments on commit 33cb82f

Please sign in to comment.