Fakultas Ilmu Komputer UI

Commit c103940d authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

add role to users schema

parent 3076237e
......@@ -6,14 +6,15 @@ defmodule Diskuy.Account.User do
field :username, :string
field :email, :string
field :picture, :string
field :role, :string, default: "reguler"
timestamps()
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:username, :email, :picture])
|> validate_required([:username, :email])
|> cast(attrs, [:username, :email, :picture, :role])
|> validate_required([:username, :email, :role])
|> validate_format(:email, ~r/^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)
|> unique_constraint(:username)
|> unique_constraint(:email)
......
......@@ -27,10 +27,14 @@ defmodule DiskuyWeb.Auth.GoogleAuth do
case Account.get_by_username(username) do
{:ok, _user} ->
new_username = create_username(username)
{:ok, user} = Account.create_user(%{username: new_username, email: email, picture: picture})
{:ok, user} = Account.create_user(%{username: new_username,
email: email,
picture: picture, role: "reguler"})
Guardian.create_token(user)
{:error, :not_found} ->
{:ok, user} = Account.create_user(%{username: username, email: email, picture: picture})
{:ok, user} = Account.create_user(%{username: username,
email: email,
picture: picture, role: "reguler"})
Guardian.create_token(user)
end
end
......
......@@ -18,7 +18,8 @@ defmodule DiskuyWeb.UserView do
%{id: user.id,
username: user.username,
name: user.email,
picture: user.picture
picture: user.picture,
role: user.role
}
end
......@@ -28,6 +29,7 @@ defmodule DiskuyWeb.UserView do
email: user.email,
username: user.username,
picture: user.picture,
role: user.role,
token: token
}
end
......
......@@ -6,6 +6,7 @@ defmodule Diskuy.Repo.Migrations.CreateUsers do
add :username, :string
add :email, :string
add :picture, :string
add :role, :string
timestamps()
end
create unique_index(:users, [:username])
......
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