Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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'})
Ejemplo n.º 4
0
def account_email_patch(args, email: str):
    action = args.get('action')
    if action is None:
        return response.unprocessable_entity('Action is required')

    if action == 'logout':
        logout_user(g.user.id)
        g.user = None
        res = response.accepted()
        res.set_cookie(key='identity_token', value='', expires=0)
        return res

    return response.bad_request('Action cannot be handled properly')