Пример #1
0
def login_api(args, email: str) -> Response:
    username = email.lower()
    password = args.get('password', '')
    if not username or not password:
        return response.bad_request('Either email or password field is empty')

    isEmail = is_valid_email(username)
    if not isEmail:
        return response.bad_request('Email address is malformed')

    isCredentialValid = is_account_credential_valid(username, password)
    if not isCredentialValid:
        return response.forbidden(
            'Email and password combination cannot be found')

    refreshed_user = login_account(username)

    res = response.ok({
        'id': refreshed_user.id,
        'email': refreshed_user.username,
        'login': refreshed_user.login_date,
        'expiry': refreshed_user.login_expiry,
    })
    res.set_cookie(
        key='identity_token',
        value=refreshed_user.token,
        expires=refreshed_user.login_expiry,
    )
    return res
Пример #2
0
def account_post(args):
    username = args.get('email', '').lower()
    password = args.get('password', '')
    if not username or not password:
        return response.bad_request(
            'Either email or password field is empty'
        )

    isEmail = is_valid_email(username)
    if not isEmail:
        return response.bad_request(
            'Email address is malformed'
        )

    user = get_user_by_email(username)
    if user is not None:
        return response.unprocessable_entity(
            'Email address has already been registered'
        )

    new_user = register_account(username, password)
    res = response.created()
    res.set_cookie(
        key='identity_token',
        value=new_user.token,
        expires=new_user.login_expiry,
    )
    return res
Пример #3
0
def account_email_get(email: str):
    isEmail = is_valid_email(email)
    if not isEmail:
        return response.bad_request('Email address is malformed')

    user = get_user_by_email(email)
    if user is None:
        return response.ok({'status': 'unregistered'})
    else:
        return response.ok({'status': 'registered'})