crusader 1639b86706 ing
2017-11-23 17:06:44 +09:00

75 lines
1.7 KiB
Go

package member
import (
"context"
"fmt"
"time"
"encoding/json"
"log"
"git.loafle.net/overflow/overflow_server_app/config"
"git.loafle.net/overflow/overflow_server_app/external/grpc"
jwt "github.com/dgrijalva/jwt-go"
"github.com/valyala/fasthttp"
)
func SignIn(ctx *fasthttp.RequestCtx) {
var err error
//signinId := string(ctx.FormValue("signinId"))
//signinPw := string(ctx.FormValue("signinPw"))
var webParams []interface{}
webBytes := ctx.PostBody()
err = json.Unmarshal(webBytes, &webParams)
if err != nil {
fmt.Fprintf(ctx, "Err!!!!: %s\n", err)
}
length := len(webParams)
if length < 0 {
fmt.Println("eeee")
}
jsonMap := webParams[0].(map[string]interface{})
signinId := jsonMap["signinId"].(string)
signinPw := jsonMap["signinPw"].(string)
params := []string{signinId, signinPw}
gRPCCtx := context.Background()
r, err := grpc.Exec(gRPCCtx, "MemberService.signin", params)
if nil != err {
fmt.Fprintf(ctx, "%v", err)
return
}
token := jwt.New(jwt.SigningMethodRS256)
/* Create a map to store our claims */
claims := token.Claims.(jwt.MapClaims)
/* Set token claims */
claims["iss"] = "overFlow"
claims["iat"] = time.Now().Unix()
claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
claims["aud"] = "www.overflow.cloud"
claims["sub"] = signinId
/* Sign the token with our secret */
tokenString, _ := token.SignedString([]byte(config.Config.Auth.SigningKey))
var jwtCookie fasthttp.Cookie
jwtCookie.SetKey("AuthToken")
jwtCookie.SetValue(tokenString)
ctx.Response.Header.SetCookie(&jwtCookie)
//fmt.Fprintf(ctx, "Welcome!!!!: %s\n", r)
ctx.SetContentType("application/javascript")
log.Printf("M:%s", r)
ctx.SetBody([]byte(r))
}