diff --git a/auth/auth.rb b/auth/auth.rb index edc814f..9cedaa2 100644 --- a/auth/auth.rb +++ b/auth/auth.rb @@ -9,12 +9,22 @@ require 'json' require 'sqlite3' require 'jwt' -payload = { data: 'test', test: 'hello' } +payload = { data: {time: 'now', help: 'no'}.to_json, test: 'hello' } hmac_secret = 'my$ecretK3y' token = JWT.encode payload, hmac_secret, 'HS256' #puts token -decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' } +begin + decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' } + puts "Token is valid!" + puts "Decoded token: #{decoded_token}" + + puts data_value = decoded_token.first['data'] + puts test_value = decoded_token.first['test'] +rescue JWT::DecodeError + puts "Invalid token or signature!" +end + #puts decoded_token initialize_database diff --git a/auth/database_queries.rb b/auth/database_queries.rb index bc05621..99b9e69 100644 --- a/auth/database_queries.rb +++ b/auth/database_queries.rb @@ -7,13 +7,13 @@ def create_new_user(username, password) db.execute(' INSERT INTO users (username, hashed_password) VALUES (?, ?) - ', [username, create_password_for_user(password)]) + ', [username.downcase, create_password_for_user(password)]) end def check_if_user_exists(username) db = SQLite3::Database.new('./database/auth.db') - result = db.get_first_value('SELECT COUNT(*) FROM users WHERE username = ?', username) + result = db.get_first_value('SELECT COUNT(*) FROM users WHERE username = ?', username.downcase) if result > 0 return true @@ -30,7 +30,7 @@ def get_user_id(username) FROM users U WHERE U.username = ? - ', [username]) + ', [username.downcase]) return user_id end