mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-01-27 10:18:54 +00:00
Add sessions
This commit is contained in:
105
apps/cf_http/lib/cf_http/sessions.ex
Normal file
105
apps/cf_http/lib/cf_http/sessions.ex
Normal file
@@ -0,0 +1,105 @@
|
||||
defmodule CfHttp.Sessions do
|
||||
@moduledoc """
|
||||
The Sessions context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias CfHttp.Repo
|
||||
|
||||
alias CfHttp.Sessions.Session
|
||||
|
||||
@doc """
|
||||
Returns the list of sessions.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_sessions()
|
||||
[%Session{}, ...]
|
||||
|
||||
"""
|
||||
def list_sessions do
|
||||
Repo.all(Session)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single session.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Session does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_session!(123)
|
||||
%Session{}
|
||||
|
||||
iex> get_session!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_session!(id), do: Repo.get!(Session, id)
|
||||
|
||||
@doc """
|
||||
Creates a session.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_session(%{field: value})
|
||||
{:ok, %Session{}}
|
||||
|
||||
iex> create_session(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_session(attrs \\ %{}) do
|
||||
%Session{}
|
||||
|> Session.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a session.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_session(session, %{field: new_value})
|
||||
{:ok, %Session{}}
|
||||
|
||||
iex> update_session(session, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_session(%Session{} = session, attrs) do
|
||||
session
|
||||
|> Session.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a session.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_session(session)
|
||||
{:ok, %Session{}}
|
||||
|
||||
iex> delete_session(session)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_session(%Session{} = session) do
|
||||
Repo.delete(session)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking session changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_session(session)
|
||||
%Ecto.Changeset{source: %Session{}}
|
||||
|
||||
"""
|
||||
def change_session(%Session{} = session) do
|
||||
Session.changeset(session, %{})
|
||||
end
|
||||
end
|
||||
|
||||
21
apps/cf_http/lib/cf_http/sessions/session.ex
Normal file
21
apps/cf_http/lib/cf_http/sessions/session.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule CfHttp.Sessions.Session do
|
||||
@moduledoc """
|
||||
Represents a Session
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "sessions" do
|
||||
field :user_id, :id
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(session, attrs \\ %{}) do
|
||||
session
|
||||
|> cast(attrs, [])
|
||||
|> validate_required([])
|
||||
end
|
||||
end
|
||||
106
apps/cf_http/lib/cf_http/users.ex
Normal file
106
apps/cf_http/lib/cf_http/users.ex
Normal file
@@ -0,0 +1,106 @@
|
||||
defmodule CfHttp.Users do
|
||||
@moduledoc """
|
||||
The Users context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias CfHttp.Repo
|
||||
|
||||
alias CfHttp.Users.User
|
||||
|
||||
@doc """
|
||||
Returns the list of users.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_users()
|
||||
[%User{}, ...]
|
||||
|
||||
"""
|
||||
def list_users do
|
||||
Repo.all(User)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single user.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the User does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_user!(123)
|
||||
%User{}
|
||||
|
||||
iex> get_user!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_user!(id), do: Repo.get!(User, id)
|
||||
|
||||
@doc """
|
||||
Creates a user.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_user(%{field: value})
|
||||
{:ok, %User{}}
|
||||
|
||||
iex> create_user(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_user(attrs \\ %{}) do
|
||||
%User{}
|
||||
|> User.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a user.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_user(user, %{field: new_value})
|
||||
{:ok, %User{}}
|
||||
|
||||
iex> update_user(user, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_user(%User{} = user, attrs) do
|
||||
user
|
||||
|> User.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a user.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_user(user)
|
||||
{:ok, %User{}}
|
||||
|
||||
iex> delete_user(user)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_user(%User{} = user) do
|
||||
Repo.delete(user)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking user changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_user(user)
|
||||
%Ecto.Changeset{source: %User{}}
|
||||
|
||||
"""
|
||||
def change_user(%User{} = user) do
|
||||
User.changeset(user, %{})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
defmodule CfHttp.User do
|
||||
defmodule CfHttp.Users.User do
|
||||
@moduledoc """
|
||||
Represents a User I guess
|
||||
"""
|
||||
23
apps/cf_http/lib/cf_http_web/templates/session/new.html.eex
Normal file
23
apps/cf_http/lib/cf_http_web/templates/session/new.html.eex
Normal file
@@ -0,0 +1,23 @@
|
||||
<main class="pa4 black-80">
|
||||
<form class="measure center">
|
||||
<fieldset id="sign_up" class="ba b--transparent ph0 mh0">
|
||||
<legend class="f4 fw6 ph0 mh0">Sign In</legend>
|
||||
<div class="mt3">
|
||||
<label class="db fw6 lh-copy f6" for="email-address">Email</label>
|
||||
<input class="pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100" type="email" name="email-address" id="email-address">
|
||||
</div>
|
||||
<div class="mv3">
|
||||
<label class="db fw6 lh-copy f6" for="password">Password</label>
|
||||
<input class="b pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100" type="password" name="password" id="password">
|
||||
</div>
|
||||
<label class="pa0 ma0 lh-copy f6 pointer"><input type="checkbox"> Remember me</label>
|
||||
</fieldset>
|
||||
<div class="">
|
||||
<input class="b ph3 pv2 input-reset ba b--black bg-transparent grow pointer f6 dib" type="submit" value="Sign in">
|
||||
</div>
|
||||
<div class="lh-copy mt3">
|
||||
<a href="#0" class="f6 link dim black db">Sign up</a>
|
||||
<a href="#0" class="f6 link dim black db">Forgot your password?</a>
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
@@ -0,0 +1,13 @@
|
||||
defmodule CfHttp.Repo.Migrations.CreateSessions do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:sessions) do
|
||||
add :user_id, references(:users, on_delete: :delete_all)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create index(:sessions, [:user_id])
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user