@@ -15,16 +15,27 @@ import (
15
15
"github.com/gin-gonic/gin"
16
16
"github.com/go-playground/validator/v10"
17
17
"go.mongodb.org/mongo-driver/bson"
18
+ "go.mongodb.org/mongo-driver/bson/primitive"
18
19
"go.mongodb.org/mongo-driver/mongo"
19
20
"golang.org/crypto/bcrypt"
21
+ "golang.org/x/crypto/bcrypt"
20
22
)
21
23
22
24
var userCollection * mongo.Collection = database .OpenCollection (database .Client , "user" )
23
25
var validate = validator .New ()
24
26
25
27
func HashPassword () {}
26
28
27
- func VerifyPassword () {}
29
+ func VerifyPassword (userPassword string , providedPassword string ) (bool , string ) {
30
+ err := bcrypt .CompareHashAndPassword ([]byte (providedPassword ), []byte (userPassword ))
31
+ check := true
32
+ msg := ""
33
+ if err != nil {
34
+ msg = fmt .Sprintf ("password is incorrect" )
35
+ check = false
36
+ }
37
+ return check , msg
38
+ }
28
39
29
40
func Signup () gin.HandlerFunc {
30
41
return func (c * gin.Context ) {
@@ -57,10 +68,53 @@ func Signup() gin.HandlerFunc {
57
68
log .Panic (err )
58
69
c .JSON (http .StatusInternalServerError , gin.H {"error" : "this email or phoneNo already exists" })
59
70
}
71
+ user .Created_at , _ = time .Parse (time .RFC3339 , time .Now ().Format (time .RFC3339 ))
72
+ user .Update_at , _ = time .Parse (time .RFC3339 , time .Now ().Format (time .RFC3339 ))
73
+ user .ID = primitive .NewObjectID ()
74
+ user .User_id = user .ID .Hex ()
75
+ token , refreshToken , err := helpers .GenerateAllTokens (* user .Email , * user .First_name , * user .Last_name , * user .User_type , * & user .User_id )
76
+ if err != nil {
77
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
78
+ return
79
+ }
80
+
81
+ user .Token = & token
82
+ user .Refresh_token = & refreshToken
83
+
84
+ //insert into db
85
+ resultInsertionNumber , insertErr := userCollection .InsertOne (ctx , user )
86
+ if insertErr != nil {
87
+ msg := fmt .Sprintf ("User item was not created" )
88
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : msg })
89
+ return
90
+ }
91
+ defer cancel ()
92
+ c .JSON (http .StatusOK , resultInsertionNumber )
60
93
}
61
94
}
62
95
63
- func Login () {}
96
+ func Login () gin.HandlerFunc {
97
+ return func (c * gin.Context ) {
98
+ var ctx , cancel = context .WithTimeout (context .Background (), 100 * time .Second )
99
+ var user models.User
100
+ var foundUser models.User
101
+
102
+ if err := c .BindJSON (& user ); err != nil {
103
+ c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
104
+ return
105
+ }
106
+
107
+ err := userCollection .FindOne (ctx , bson.M {"email" : user .Email }).Decode (& foundUser ) //store after decoding
108
+ defer cancel ()
109
+ if err != nil {
110
+ c .JSON (http .StatusBadRequest , gin.H {"error" : "email is incorrect" })
111
+ return
112
+ }
113
+
114
+ passwordIsValid , msg := VerifyPassword (* user .Password , * foundUser .Password )
115
+ defer cancel ()
116
+ }
117
+ }
64
118
65
119
func GetUsers () {}
66
120
0 commit comments