Require users to Log in¶
AdminUI comes with a login page. To enable it, specify a function to handle login:
@app.login()
def on_login(username, password):
if username=='alice' and password=='123456':
return LoggedInUser("Alice")
else:
return LoginFailed()
The function will receive the username and password users inputted; you need to return LoggedInUser or LoginFailed depending on the result. Instead of a simple if statement, typically you need to check the credential against a database or something.
If you want to redirect logged in user to a different page, set the redirect_to argument in LoggedInUser, like:
return LoggedInUser("Alice", redirect_to='/detail')
The returned LoggedInUser and LoginFailed may contain more information:
-
class
adminui.
LoggedInUser
(display_name='', auth=['user'], avatar='https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png', user_info=None, redirect_to=None)¶ Returned by login handler, represent a successfully logged in user.
参数: - display_name – the display name of the user
- auth – a list of permission string the user have. Will be checked against in pages or menus
- avatar – the avatar of the user
- user_info – info for future use, accessible by app.current_user()[‘user_info’]
-
class
adminui.
LoginFailed
(title='Login Failed', message='Username or password is incorrect')¶ Returned by login handler, represent a failed login attempt
参数: - title – the title shown in the error message. default: ‘Login Failed’
- message – the error message content. default: ‘Username or password is incorrect’
Pages requires authorization¶
By default, any user can visit the page you described. If you want to show the page to users only with certain permission, add an auth_needed attribute to your page:
@app.page('/', 'Logged In', auth_needed='user')
def form_page():
return [
Card('Logged In', [
Header('You are logged in')
])
]
In this way, only logged in users can visit this page. Other users will be redirected to the login page.
You may also use auth_needed=’admin’, then a user logged in with:
LoggedInUser("Alice", auth=['user', 'admin'])
May access this page, since the user Alice has ‘admin’ authority.
Forget password link and register link¶
You may change the forget password link, or register link on the login page, like:
app.register_link={'Sign Up': '/signup'}
app.forget_password_link={'Forget Password': '/forget'}
If they are set to None
, they won’t show up on the screen.