forked from vlang/gitly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.v
44 lines (36 loc) · 976 Bytes
/
token.v
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
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
struct Token {
id int
user_id int
value string
ip string
}
fn (mut app App) update_user_token(user_id int, token string, ip string) string {
tok := app.find_user_token(user_id, ip)
if tok == '' {
new_token := Token{user_id: user_id, value: token, ip: ip }
sql app.db {
insert new_token into Token
}
return token
}
return tok
}
fn (mut app App) find_user_token(user_id int, ip string) string {
tok := sql app.db {
select from Token where user_id == user_id && ip == ip limit 1
}
return tok.value
}
fn (mut app App) clear_sessions(user_id int) {
sql app.db {
delete from Token where user_id == user_id
}
}
fn (mut app App) add_token(user_id int, ip string) string {
mut token := gen_uuid_v4ish()
token = app.update_user_token(user_id, token, ip)
return token
}