コード例 #1
0
ファイル: user.py プロジェクト: richmonkey/bauhinia_api
def register_user():
    if not request.data:
        return INVALID_PARAM()
    
    req = json.loads(request.data)
    name = req.get('nickname')
    password = req.get('password')
    #短信验证码
    code = req.get("code")
    number = req.get("number")
    country_code = req.get("country_code")

    if not name or not password or not code \
       or not number or not country_code:
        return INVALID_PARAM()
    
    #check sms code
    if is_test_number(number):
        pass
    else:
        c2, timestamp, _ = code.get_verify_code(g.rds, country_code, number)
        if c1 != c2:
            return INVALID_CODE()
    password = generate_password_hash(password)
    phone_number = "+%s-%s"%(country_code, number)
    u = DBUser.get_user(g._db, phone_number)
    if u:
        uid = u['id']
        DBUser.save_user(g._db, uid, name, password)
    else:
        uid = DBUser.add_user(g._db, name, password, phone_number)

    #登录动作
    access_token = gobelieve.login_gobelieve(uid, name)
    if not access_token:
        return CAN_NOT_GET_TOKEN()

    tok = {
        'expires_in': 3600,
        "access_token":access_token,
        "refresh_token":random_token_generator(),
        'uid':uid
    }


    Token.save_access_token(g.rds, access_token, uid, 3600)
    Token.save_refresh_token(g.rds, tok['refresh_token'], uid)

    return make_response(200, tok)
コード例 #2
0
ファイル: user.py プロジェクト: richmonkey/bauhinia_api
def login():
    if not request.data:
        return INVALID_PARAM()
    
    req = json.loads(request.data)

    password = req.get('password')
    number = req.get("number")
    country_code = req.get("country_code")

    phone_number = "+%s-%s"%(country_code, number)
    u = DBUser.get_user(g._db, phone_number)
    if not u:
        return INVALID_USERNAME()
    if not check_password_hash(u['password'], password):
        return INVALID_PASSWORD()

    uid = u['id']
    nickname = u.get('nickname')
    avatar = u.get('avatar')
    state = u.get('state')
    nickname = nickname if nickname else ""
    avatar = avatar if avatar else ""
    state = state if state else ""
    
    access_token = gobelieve.login_gobelieve(uid, nickname)
        
    if not access_token:
        return CAN_NOT_GET_TOKEN()

    tok = {
        'expires_in': 3600,
        "access_token":access_token,
        "refresh_token":random_token_generator(),
        'uid':u['id'],
        'avatar':avatar,
        'state':state
    }

    Token.save_access_token(g.rds, access_token, u['id'], 3600)
    Token.save_refresh_token(g.rds, tok['refresh_token'], u['id'])
    
    return make_response(200, tok)