Commit 922a4179 authored by Bat's avatar Bat
Browse files

Authentication

parent ed9c5f73
......@@ -31,10 +31,19 @@ Amber::Server.configure do |app|
routes :web do
get "/", HomeController, :index
get "/configure", HomeController, :show_configure
post "/configure", HomeController, :configure
get "/@:name", AccountController, :show
get "/register", AccountController, :new
post "/register", AccountController, :create
get "/signin", SessionController, :new
post "/signin", SessionController, :create
get "/logout", SessionController, :delete
get "/blog", ActivityPub::BlogController, :show
get "/~:name", BlogController, :show
......
-- +micrate Up
ALTER TABLE accounts ADD COLUMN email VARCHAR;
ALTER TABLE accounts ADD COLUMN hashed_password VARCHAR;
-- +micrate Down
ALTER TABLE accounts DROP COLUMN email;
ALTER TABLE accounts DROP COLUMN hashed_password;
require "./spec_helper"
describe SessionController do
end
class AccountController < ApplicationController
include ActivityPub::Utils
def show
account = Account.from_name(make_actor_id(params["name"]))
title = "@" + account.actor_id.not_nil!
render("index.ecr")
end
def new
title = params["title"] || "Register"
render("new.ecr")
end
def create
acct = Account.new
acct.actor_id = make_actor_id(params["username"])
acct.email = params["email"]
acct.password = params["password"]
acct.display_name = params["username"]
acct.bio = ""
Repo.insert(acct)
redirect_to(controller: :account, action: :show, params: {
"name" => acct.actor_id.not_nil!
})
end
end
......@@ -3,4 +3,8 @@ require "jasper_helpers"
class ApplicationController < Amber::Controller::Base
include JasperHelpers
LAYOUT = "application.ecr"
def get_account
Repo.get(Account, session[:account_id].to_s.to_i).not_nil!
end
end
......@@ -7,7 +7,11 @@ class HomeController < ApplicationController
def show_configure
title = "Configuration"
render("initial_config.ecr")
if Instance.initialized?
render("config_success.ecr")
else
render("initial_config.ecr")
end
end
def configure
......@@ -17,6 +21,8 @@ class HomeController < ApplicationController
Repo.insert(instance)
puts "INSTANCE :"
puts Instance.get
redirect_to(controller: :home, action: :index, status: 302)
redirect_to(controller: :account, action: :new, status: 302, params: {
"title" => "Create the first user"
})
end
end
class SessionController < ApplicationController
def new
account = Account.new
title = "Sign In"
render("new.ecr")
end
def create
account = Account.find_by_email(params["email"].to_s)
if account && account.authenticate(params["password"].to_s)
session[:account_id] = account.id
flash[:info] = "Successfully logged in"
redirect_to "/"
else
flash[:danger] = "Invalid email or password"
title = "Sign In"
account = Account.new
render("new.ecr")
end
end
def delete
session.delete(:account_id)
flash[:info] = "Logged out. See ya later!"
redirect_to "/"
end
end
require "crypto/bcrypt/password"
class Account < Crecto::Model
include Crypto
private getter new_password : String?
schema "accounts" do
field :actor_id, String
field :email, String
field :hashed_password, String
field :display_name, String
field :bio, String
has_many :posts, Post, dependent: :destroy
belongs_to :instance, Instance
end
def self.from_name(name : String)
Repo.all(Account, Query.where(actor_id: name).limit(1))[0]
end
def self.find_by_email(email : String)
Repo.all(Account, Query.where(email: email).limit(1))[0]
end
def password=(password)
@new_password = password
@hashed_password = Bcrypt::Password.create(password, cost: 10).to_s
end
def password
(hash = hashed_password) ? Bcrypt::Password.new(hash) : nil
end
def password_changed?
new_password ? true : false
end
def valid_password_size?
(pass = new_password) ? pass.size >= 8 : false
end
def authenticate(password : String)
(bcrypt_pass = self.password) ? bcrypt_pass == password : false
end
end
class HTTP::Server::Context
property current_account : Account?
end
class Authenticate < Amber::Pipe::Base
def call(context)
account_id = context.session["account_id"]?
if account = Repo.get(Account, account_id)
context.current_account = account
call_next(context)
else
context.flash[:warning] = "Please Sign In"
context.response.headers.add "Location", "/signin"
context.response.status_code = 302
end
end
end
<h1><%= title %></h1>
<main>
<h1>New Account</h1>
<form method="POST" action="/register/">
<label for="username">Username</label>
<input name="username">
<label for="email">Email</label>
<input type="email" name="email">
<label for="password">Password</label>
<input type="password" name="password">
<%= csrf_tag %>
<input type="submit" value="Register">
</form>
</main>
<h1>Your instance is correctly configured!</h1>
<h1><%= instance.name %></h1>
<% if get_account != nil %>
<p>Welcome <%= get_account.display_name %>!</p>
<% end %>
......@@ -14,4 +14,4 @@
<input type="submit" value="Finalize instance creation">
</form>
</main>
\ No newline at end of file
</main>
<main>
<h1>Sign In</h1>
<form action="/signin/" method="POST">
<label for="email">Email</label>
<input type="email" name="email">
<label for="password">Password</label>
<input type="password" name="password">
<%= csrf_tag %>
<input type="submit" value="Sign In">
</form>
<%= link_to("Don't have an account yet?", "/register") %>
</main>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment