-
Notifications
You must be signed in to change notification settings - Fork 57
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
add github login #101
base: master
Are you sure you want to change the base?
add github login #101
Conversation
lib/elixir_china/github.ex
Outdated
end | ||
|
||
def authorize_url! do | ||
OAuth2.Client.authorize_url!(client(), scope: "user,public_repo") |
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.
这里的权限要的太多了吧? 我试了一下,按你现在写的代码,应该用 user:email
就行了。 https://developer.github.com/v3/oauth/#scopes
lib/elixir_china/github.ex
Outdated
OAuth2.Client.authorize_url!(client(), scope: "user,public_repo") | ||
end | ||
|
||
def get_token!(params \\ [], headers \\ [], opts \\ []) do |
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.
不太建议这里用这么多可选参数,使用起来选择太多,发而不太好用。而且你代码里其实也基本没用到什么默认参数的功能。可以把常用的参数作为必须的,比如 params
。剩下的再考虑是可选吧
web/controllers/auth_controller.ex
Outdated
email && (user = Repo.get_by(User, email: email)) -> | ||
{:ok, user} | ||
email && name && Repo.get_by(User, name: name) -> | ||
{:ok, insert_user(email, generate_name(name))} |
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.
这里除了你这种做法(先根据 name 查 user),我想到还有一种做法是先直接插入数据,因为 name 有唯一索引,如果插入失败再生成 name 来创建,这样如果用户名不存在的话,就减少了查询。你觉得哪种好点?
web/templates/layout/app.html.eex
Outdated
@@ -39,6 +39,9 @@ | |||
<%= render @view_module, @view_template, assigns %> | |||
</div> | |||
<sidebar class="col-md-3" role="sidebar"> | |||
<%= if @user_id == nil do %> | |||
<%= render ElixirChina.SharedView, "_social_login.html", conn: @conn %> |
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.
我更倾向于把这个页面放在登录那里,不然到处都有这个登录按钮
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.
额,确实有点简单粗暴了,还是放在登录界面里面好一点。
web/controllers/auth_controller.ex
Outdated
with {:ok, user} <- insert_user(email, name) do | ||
{:ok, user} | ||
else | ||
{:error, %Ecto.Changeset{errors: [email: {"has already been taken", []}]}} -> |
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.
这里我倒觉得可以直接先按 email 来查,毕竟插入的操作不需要做很多,用户通过 GitHub 登录一次后,以后都可以通过 email 找到。
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.
Ya. 再修改。
3b8344d
to
4678982
Compare
No description provided.