Ejemplo n.º 1
0
def callback_facebook(resp):
    if resp is None:
        return error_login(
            "Access denied: reason=%s error=%s" % (request.args["error_reason"], request.args["error_description"])
        )

    user_tmp = Usuario()
    user_tmp.oauth_token = resp["access_token"]
    g.user = user_tmp

    me = auth.facebook.get("/me")
    user = Usuario.query.filter(Usuario.nickname == me.data["name"]).filter(Usuario.email == me.data["email"]).first()

    if user is None:
        user = Usuario()
        user.email = me.data["email"]
        user.oauth_provider = "facebook"
        user.nickname = me.data["name"]
        db.session.add(user)
    user.oauth_token = resp["access_token"]
    db.session.commit()

    session["user_id"] = user.id
    session["oauth_provider"] = user.oauth_provider
    g.user = user
    return redirect(url_for("home"))
Ejemplo n.º 2
0
def callback_twitter(resp):
    next_url = request.args.get("next") or url_for("home")
    if resp is None:
        error_login(u"You denied the request to sign in.")
        return redirect(next_url)

    user = Usuario.query.filter_by(nickname=resp["screen_name"]).first()

    if user is None:
        user = Usuario()
        user.nickname = resp["screen_name"]
        user.oauth_provider = "twitter"
        db.session.add(user)

    user.oauth_token = resp["oauth_token"]
    user.oauth_token_secret = resp["oauth_token_secret"]

    db.session.commit()

    session["user_id"] = user.id
    session["oauth_provider"] = user.oauth_provider
    g.user = user

    return redirect(url_for("home"))