Last active
June 22, 2016 17:15
-
-
Save jpbecotte/e59392e09f0ff5dce232c90f897d54a4 to your computer and use it in GitHub Desktop.
Elixir code exemple with unsafe (user) variable to be ported for version 1.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create(conn, %{"fb_token" => fb_token, "version" => version}) do #from API | |
if ! version_check(conn, version) do | |
send_bad_version(conn) | |
else | |
Facebook.Config.appsecret "xxxxxxxx" | |
Facebook.Config.graph_url "https://graph.facebook.com/" | |
{:json, data} = Facebook.me("email,name", fb_token) | |
Logger.debug "FB API CALL returns: #{inspect(data)}" | |
user = Repo.get_by(User, facebook_id: data["id"]) | |
if user == nil do | |
changeset = User.changeset_fb(%User{}, %{ | |
name: data["name"], | |
email: data["email"], | |
facebook_id: data["id"] | |
}) | |
if changeset.valid? do | |
{:ok, user} = Repo.insert changeset | |
end | |
end | |
if user != nil do | |
conn | |
|> put_session(:current_user, user.id) | |
|> json(%{ | |
status: "authorized", | |
id: user.id, | |
email: user.email, | |
name: user.name, | |
socket_token: Phoenix.Token.sign(conn, "user", user.id) | |
}) | |
else | |
conn | |
|> json(%{status: "something went wrong"}) | |
end | |
end | |
end |
frahugo
commented
Jun 22, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment