-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ro auth wip #73
base: master
Are you sure you want to change the base?
Ro auth wip #73
Changes from all commits
fd5c982
c233ca8
4dd4dd8
b47133f
0aa9291
4c14347
46876f3
c8dfd6a
7440407
2f1fbe0
6bdf79f
616ce21
05bcd16
b9cb236
e0e1742
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Quack.Password do | ||
alias Quack.Repo | ||
import Ecto.Changeset, only: [put_change: 3] | ||
import Comeonin.Bcrypt, only: [hashpwsalt: 1] | ||
|
||
@doc """ | ||
Encrypts password in the user changeset and stores it to the changeset as `encrypted_password`. | ||
""" | ||
def encrypt_password(changeset) do | ||
put_change(changeset, :encrypted_password, hashpwsalt(changeset.params["password"])) | ||
end | ||
|
||
@doc """ | ||
Encrypts password in the user changeset and stores it to the database. | ||
""" | ||
def encrypt_password_and_save_user(changeset) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on using https://github.com/elixircnx/comeonin/blob/ba206cc811766ef3ed8c1afe40fdbd4ef86f2f7b/lib/comeonin.ex#L182 ? Ed: I prefer doing it your way actually. |
||
changeset | ||
|> encrypt_password | ||
|> Repo.insert | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule Quack.Repo.Migrations.AddFieldsToUser do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:users) do | ||
add :email, :string | ||
add :password_encrypted, :string | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both columns need indexes. Probably should be null: false also. |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Quack.RegistrationController do | ||
use Quack.Web, :controller | ||
|
||
alias Quack.Password | ||
|
||
plug :scrub_params, "user" when action in [:create] | ||
|
||
def create(conn, params) do | ||
changeset = User.changeset(%User{}, params["user"]) | ||
|
||
if changeset.valid? do | ||
new_user = Password.encrypt_password_and_save_user(changeset) | ||
|
||
conn | ||
|> put_flash(:info, "Successfully registered and logged in") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does put_flash work in an xhr? Or this is wip still? |
||
|> put_session(:current_user, new_user) | ||
|> redirect(to: page_path(conn, :index)) | ||
else | ||
# render json error | ||
json conn, %{error: "WHOOPS!"} | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,17 @@ defmodule Quack.User do | |
use Quack.Web, :model | ||
|
||
schema "users" do | ||
field :email, :string | ||
field :name, :string | ||
field :password_encrypted, :string | ||
field :password, :string, virtual: true | ||
field :password_confirmation, :string, virtual: true | ||
has_many :messages, Message | ||
|
||
timestamps | ||
end | ||
|
||
@required_fields ~w(name) | ||
@required_fields ~w(email name password password_confirmation) | ||
@optional_fields ~w() | ||
|
||
@doc """ | ||
|
@@ -20,5 +24,8 @@ defmodule Quack.User do | |
def changeset(model, params \\ :empty) do | ||
model | ||
|> cast(params, @required_fields, @optional_fields) | ||
|> validate_unique(:email, on: Quack.Repo, downcase: true) | ||
|> validate_length(:password, min: 6) | ||
|> validate_confirmation(:password, message: "passwords do not match") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoa! Didn't know this existed. Cool. |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var React = require('react'); | ||
var Actions = require('../actions/actions'); | ||
var Navigation = require('react-router').Navigation; | ||
var KeyCodes = require('../utils/keycodes'); | ||
|
||
var Register = React.createClass({ | ||
mixins: [Navigation], | ||
|
||
render: function () { | ||
return ( | ||
<article className="register"> | ||
<input type="text" placeholder="email" onChange={this._onChange.bind(this, 'email')} onKeyDown={this._onKeyDown} /> | ||
<input type="password" placeholder="password" onChange={this._onChange.bind(this, 'password')} onKeyDown={this._onKeyDown} /> | ||
<a className="quack" onClick={this._onClick} href="#">Register</a> | ||
</article> | ||
); | ||
}, | ||
|
||
getInitialState: function() { | ||
return { | ||
email: "", | ||
password: "" | ||
}; | ||
}, | ||
|
||
_onChange: function(field, event) { | ||
event.preventDefault(); | ||
var newState = {}; | ||
newState[field] = event.target.value; | ||
this.setState(newState); | ||
}, | ||
|
||
_onClick: function(e) { | ||
e.preventDefault(); | ||
this._registerUser(); | ||
}, | ||
|
||
_onKeyDown: function(event) { | ||
if (event.keyCode === KeyCodes.enter) { | ||
event.preventDefault(); | ||
this._registerUser(); | ||
} | ||
}, | ||
|
||
_register: function() { | ||
Actions.registerUser({ | ||
email: this.state.email, | ||
password: this.state.password | ||
}); | ||
|
||
// this.transitionTo('chat'); | ||
} | ||
|
||
}); | ||
|
||
module.exports = Register; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var $ = require('jquery'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to use superagent to do ajax requests: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I believe we can use the ajax functionality in phoenix.js |
||
|
||
var QuackAPI = { | ||
post: function(params) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this always posts to /register lets put that in the name. |
||
$.ajax({ | ||
type: 'post', | ||
url: '/api/register', | ||
data: params, | ||
success: function() { | ||
console.log('Success!'); | ||
}, | ||
error: function() { | ||
console.log('Error! :('); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = QuackAPI; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defp instead of def?