mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples: refactor vweb_orm_jwt (#15389)
This commit is contained in:
parent
9c96b13f9b
commit
b45da86688
1
examples/vweb_orm_jwt/.gitignore
vendored
1
examples/vweb_orm_jwt/.gitignore
vendored
@ -8,3 +8,4 @@ v
|
||||
*.dll
|
||||
vls.log
|
||||
.env
|
||||
*.db
|
||||
|
@ -15,11 +15,11 @@ struct JwtHeader {
|
||||
}
|
||||
|
||||
struct JwtPayload {
|
||||
sub string // (subject) = Entidade à quem o token pertence, normalmente o ID do usuário;
|
||||
iss string // (issuer) = Emissor do token;
|
||||
exp string // (expiration) = Timestamp de quando o token irá expirar;
|
||||
iat time.Time // (issued at) = Timestamp de quando o token foi criado;
|
||||
aud string // (audience) = Destinatário do token, representa a aplicação que irá usá-lo.
|
||||
sub string // (subject) = Entity to whom the token belongs, usually the user ID;
|
||||
iss string // (issuer) = Token issuer;
|
||||
exp string // (expiration) = Timestamp of when the token will expire;
|
||||
iat time.Time // (issued at) = Timestamp of when the token was created;
|
||||
aud string // (audience) = Token recipient, represents the application that will use it.
|
||||
name string
|
||||
roles string
|
||||
permissions string
|
||||
@ -42,7 +42,7 @@ fn (mut app App) service_auth(username string, password string) ?string {
|
||||
return error('user is not active')
|
||||
}
|
||||
|
||||
db.close()
|
||||
db.close()?
|
||||
|
||||
bcrypt.compare_hash_and_password(password.bytes(), user.password.bytes()) or {
|
||||
return error('Failed to auth user, $err')
|
||||
|
@ -0,0 +1,8 @@
|
||||
module databases
|
||||
|
||||
import sqlite
|
||||
|
||||
pub fn create_db_connection() ?sqlite.DB {
|
||||
mut db := sqlite.connect('database.db')?
|
||||
return db
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
module databases
|
||||
|
||||
import mysql
|
||||
import os
|
||||
|
||||
pub fn create_db_connection() ?mysql.Connection {
|
||||
mut db := mysql.Connection{
|
||||
host: os.getenv('DB_HOST')
|
||||
port: os.getenv('DB_PORT').u32()
|
||||
username: os.getenv('DB_USERNAME')
|
||||
password: os.getenv('DB_PASSWORD')
|
||||
dbname: os.getenv('DB_NAME')
|
||||
}
|
||||
|
||||
db.connect() or { println(err) }
|
||||
|
||||
return db
|
||||
}
|
@ -18,7 +18,7 @@ fn main() {
|
||||
create table User
|
||||
}
|
||||
|
||||
db.close()
|
||||
db.close() or { panic(err) }
|
||||
|
||||
vweb.run(new_app(), http_port)
|
||||
}
|
||||
|
@ -61,12 +61,12 @@ pub fn (mut app App) delete() vweb.Result {
|
||||
}
|
||||
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
|
||||
sql db {
|
||||
drop table User
|
||||
}
|
||||
|
||||
return app.text('Tabela deletada com sucesso')
|
||||
return app.text('Successfully deleted table')
|
||||
}
|
||||
|
@ -1,16 +1,13 @@
|
||||
module main
|
||||
|
||||
import time
|
||||
|
||||
[table: 'usersxqa']
|
||||
[table: 'users']
|
||||
struct User {
|
||||
mut:
|
||||
id int [primary; sql: serial]
|
||||
username string [required; sql_type: 'varchar(191)']
|
||||
password string [required; sql_type: 'longtext']
|
||||
name string [sql_type: 'varchar(191)']
|
||||
created_at time.Time [sql_type: 'datetime(3)']
|
||||
updated_at time.Time [sql_type: 'datetime(3)']
|
||||
deleted_at time.Time [sql_type: 'datetime(3)']
|
||||
username string [required; sql_type: 'TEXT']
|
||||
password string [required; sql_type: 'TEXT']
|
||||
created_at string [default: 'CURRENT_TIMESTAMP']
|
||||
updated_at string [default: 'CURRENT_TIMESTAMP']
|
||||
deleted_at string [default: 'CURRENT_TIMESTAMP']
|
||||
active bool
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ module main
|
||||
|
||||
import crypto.bcrypt
|
||||
import databases
|
||||
import time
|
||||
|
||||
fn (mut app App) service_add_user(username string, password string) ?User {
|
||||
mut db := databases.create_db_connection() or {
|
||||
@ -11,7 +10,7 @@ fn (mut app App) service_add_user(username string, password string) ?User {
|
||||
}
|
||||
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
|
||||
hashed_password := bcrypt.generate_from_password(password.bytes(), bcrypt.min_cost) or {
|
||||
@ -21,11 +20,7 @@ fn (mut app App) service_add_user(username string, password string) ?User {
|
||||
|
||||
user_model := User{
|
||||
username: username
|
||||
name: password
|
||||
password: hashed_password
|
||||
created_at: time.now()
|
||||
updated_at: time.now()
|
||||
deleted_at: time.now()
|
||||
active: true
|
||||
}
|
||||
|
||||
@ -47,7 +42,7 @@ fn (mut app App) service_get_user_by_id(user_id int) ?User {
|
||||
}
|
||||
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
|
||||
results := sql db {
|
||||
@ -64,7 +59,7 @@ fn (mut app App) service_get_all_user() ?[]User {
|
||||
}
|
||||
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
|
||||
results := sql db {
|
||||
@ -81,7 +76,7 @@ fn (mut app App) service_get_by_username(username string) ?User {
|
||||
}
|
||||
|
||||
defer {
|
||||
db.close()
|
||||
db.close() or { panic(err) }
|
||||
}
|
||||
|
||||
results := sql db {
|
||||
@ -89,7 +84,7 @@ fn (mut app App) service_get_by_username(username string) ?User {
|
||||
}
|
||||
|
||||
if results.len == 0 {
|
||||
return error('Usuário não encontrado')
|
||||
return error('User not found')
|
||||
}
|
||||
|
||||
return results[0]
|
||||
|
Loading…
Reference in New Issue
Block a user