Browse Source

Improve registering new users with default value and redirection to today

pull/59/head v0.4.0
Arthur POULET 12 months ago
parent
commit
c240071b4e
Signed by: Sceptique GPG Key ID: C108A668349EE601
  1. 14
      config/default_pexs_for_new_users.yaml
  2. 12
      src/users.rb

14
config/default_pexs_for_new_users.yaml

@ -0,0 +1,14 @@
- name: A mis le tableau à jour hier
amount: 3
- category: social
name: Inviter quelqu’un
amount: 3
- category: vie
name: Lire un chapitre de livre
amount: 1
- category: sport
name: Chaque série simple
amount: 1
- category: sport
name: Enchaine 3 séries simples
amount: 3

12
src/users.rb

@ -1,6 +1,10 @@
require "yaml"
class UserSystem < AuthSystem
use AuthSystem
DEFAULT_PEXS_FOR_NEW_USERS = YAML.load_file "config/default_pexs_for_new_users.yaml"
get "/login" do
slim :login
end
@ -9,8 +13,7 @@ class UserSystem < AuthSystem
user = LifePex::User.where(username: params["username"]).first
if user && BCrypt::Password.new(user[:hashed_password]) == params["password"]
cookies["auth"] = JWT.encode({ "user_id" => user[:id] }, LifePex::SECRET)
# redirect "/"
slim :login, locals: { flash: { warning: "Logged in but no page yet" } }
redirect "/"
else
slim :login, locals: { flash: { danger: "Failed to login" } }
end
@ -26,7 +29,10 @@ class UserSystem < AuthSystem
hashed_password = BCrypt::Password.create params["password"]
user_id = LifePex::User.insert(username: params["username"], hashed_password: hashed_password)
cookies["auth"] = JWT.encode({ "user_id" => user_id }, LifePex::SECRET)
slim :register, locals: { flash: { warning: "Logged in but no page yet" } }
DEFAULT_PEXS_FOR_NEW_USERS.each do |pex|
LifePex::Pex.insert(**pex, user_id: user_id)
end
redirect "/"
else
slim :register, locals: { flash: { danger: "Failed to register as #{params['username']}" } }
end

Loading…
Cancel
Save