1
0
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:
Hitalo de Jesus do Rosário Souza 2022-08-12 11:24:57 -03:00 committed by GitHub
parent 9c96b13f9b
commit b45da86688
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 48 deletions

View File

@ -8,3 +8,4 @@ v
*.dll *.dll
vls.log vls.log
.env .env
*.db

View File

@ -15,11 +15,11 @@ struct JwtHeader {
} }
struct JwtPayload { struct JwtPayload {
sub string // (subject) = Entidade à quem o token pertence, normalmente o ID do usuário; sub string // (subject) = Entity to whom the token belongs, usually the user ID;
iss string // (issuer) = Emissor do token; iss string // (issuer) = Token issuer;
exp string // (expiration) = Timestamp de quando o token irá expirar; exp string // (expiration) = Timestamp of when the token will expire;
iat time.Time // (issued at) = Timestamp de quando o token foi criado; iat time.Time // (issued at) = Timestamp of when the token was created;
aud string // (audience) = Destinatário do token, representa a aplicação que irá usá-lo. aud string // (audience) = Token recipient, represents the application that will use it.
name string name string
roles string roles string
permissions string permissions string
@ -42,7 +42,7 @@ fn (mut app App) service_auth(username string, password string) ?string {
return error('user is not active') return error('user is not active')
} }
db.close() db.close()?
bcrypt.compare_hash_and_password(password.bytes(), user.password.bytes()) or { bcrypt.compare_hash_and_password(password.bytes(), user.password.bytes()) or {
return error('Failed to auth user, $err') return error('Failed to auth user, $err')

View File

@ -0,0 +1,8 @@
module databases
import sqlite
pub fn create_db_connection() ?sqlite.DB {
mut db := sqlite.connect('database.db')?
return db
}

View File

@ -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
}

View File

@ -18,7 +18,7 @@ fn main() {
create table User create table User
} }
db.close() db.close() or { panic(err) }
vweb.run(new_app(), http_port) vweb.run(new_app(), http_port)
} }

View File

@ -61,12 +61,12 @@ pub fn (mut app App) delete() vweb.Result {
} }
defer { defer {
db.close() db.close() or { panic(err) }
} }
sql db { sql db {
drop table User drop table User
} }
return app.text('Tabela deletada com sucesso') return app.text('Successfully deleted table')
} }

View File

@ -1,16 +1,13 @@
module main module main
import time [table: 'users']
[table: 'usersxqa']
struct User { struct User {
mut: mut:
id int [primary; sql: serial] id int [primary; sql: serial]
username string [required; sql_type: 'varchar(191)'] username string [required; sql_type: 'TEXT']
password string [required; sql_type: 'longtext'] password string [required; sql_type: 'TEXT']
name string [sql_type: 'varchar(191)'] created_at string [default: 'CURRENT_TIMESTAMP']
created_at time.Time [sql_type: 'datetime(3)'] updated_at string [default: 'CURRENT_TIMESTAMP']
updated_at time.Time [sql_type: 'datetime(3)'] deleted_at string [default: 'CURRENT_TIMESTAMP']
deleted_at time.Time [sql_type: 'datetime(3)']
active bool active bool
} }

View File

@ -2,7 +2,6 @@ module main
import crypto.bcrypt import crypto.bcrypt
import databases import databases
import time
fn (mut app App) service_add_user(username string, password string) ?User { fn (mut app App) service_add_user(username string, password string) ?User {
mut db := databases.create_db_connection() or { mut db := databases.create_db_connection() or {
@ -11,7 +10,7 @@ fn (mut app App) service_add_user(username string, password string) ?User {
} }
defer { defer {
db.close() db.close() or { panic(err) }
} }
hashed_password := bcrypt.generate_from_password(password.bytes(), bcrypt.min_cost) or { 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{ user_model := User{
username: username username: username
name: password
password: hashed_password password: hashed_password
created_at: time.now()
updated_at: time.now()
deleted_at: time.now()
active: true active: true
} }
@ -47,7 +42,7 @@ fn (mut app App) service_get_user_by_id(user_id int) ?User {
} }
defer { defer {
db.close() db.close() or { panic(err) }
} }
results := sql db { results := sql db {
@ -64,7 +59,7 @@ fn (mut app App) service_get_all_user() ?[]User {
} }
defer { defer {
db.close() db.close() or { panic(err) }
} }
results := sql db { results := sql db {
@ -81,7 +76,7 @@ fn (mut app App) service_get_by_username(username string) ?User {
} }
defer { defer {
db.close() db.close() or { panic(err) }
} }
results := sql db { results := sql db {
@ -89,7 +84,7 @@ fn (mut app App) service_get_by_username(username string) ?User {
} }
if results.len == 0 { if results.len == 0 {
return error('Usuário não encontrado') return error('User not found')
} }
return results[0] return results[0]