-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_in.go
65 lines (53 loc) · 1.64 KB
/
sign_in.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"fmt"
"net/http"
)
// MessagePost: POST
func SignIn(w http.ResponseWriter, r *http.Request) {
// We need to use to use the struct model to map the json data to
type ResponseBody struct {
Token string `json:"token"`
}
var jsonResponse UserSignup
// We decode the incoming data and convert it to a json
json.NewDecoder(r.Body).Decode(&jsonResponse)
println("incoming user data ", jsonResponse.Name, jsonResponse.Pin)
// if user exists process to the next step
if RedisClient.HExists("users", jsonResponse.Name).Val() == true {
// Check if user and password before giving a token
redisUser, err := RedisClient.HGet("users", jsonResponse.Name).Result()
if err != nil {
println(err)
}
fmt.Println("Got the user", redisUser)
// Unmarshal the Json from Redis
var redisJson UserSignup
bytes := []byte(redisUser)
err2 := json.Unmarshal(bytes, &redisJson)
if err2 != nil {
panic(err2)
}
println("Redis user data ", redisJson.Name, redisJson.Pin)
// checkName := jsonResponse.Name != redisJson.Name
if redisJson.Pin == jsonResponse.Pin && jsonResponse.Name == redisJson.Name {
println("user and password match send a tooken")
// Password is correct send him/her a token brother
json.NewEncoder(w).Encode(ResponseBody{
Token: CreateToken(jsonResponse.Name),
})
} else {
println("Not matching user and password ")
// user pass is wrong send back message
json.NewEncoder(w).Encode(UserResponse{
Message: "Invalid Credentials",
})
}
} else {
json.NewEncoder(w).Encode(UserResponse{
Message: "Invalid Credentials",
User: jsonResponse.Name,
})
}
}