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