Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

authentication issues with on_air (RedirectResponse not working properly) #1517

Closed
Smug246 opened this issue Aug 29, 2023 · 20 comments
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Smug246
Copy link
Contributor

Smug246 commented Aug 29, 2023

Description

when on_air=True authentication doesnt seem to work.

minimal reproduction:

from nicegui import ui, app
from fastapi.responses import RedirectResponse
import asyncio

primary_color = '#13b27c'

info = {'admin': 'admin'} 

@ui.page('/')
def confirm_delivery():
    if not app.storage.user.get('authenticated', False):
        return RedirectResponse('login')

@ui.page('/login')
def login():
    async def try_login():
        input_username = username.value
        input_password = password.value

        if input_username in info and info[input_username] == input_password: 
            app.storage.user.update({'username': input_username, 'authenticated': True}) 
            ui.notify('Logged In', type='success')
            await asyncio.sleep(1.5)
            ui.open('/')
        else:
            ui.notify('Incorrect Username or Password', type='error')
            username.set_value('')
            password.set_value('')

    with ui.card():
        username = ui.input('Enter Your Username')
        password = ui.input('Enter Your Password', password=True, password_toggle_button=True).on('keydown.enter', try_login)
        ui.button('Log In', on_click=try_login, color=primary_color)

ui.run(title='test', storage_secret='test', on_air=True)
@falkoschindler
Copy link
Contributor

@Smug246
Can you, please, try to reduce the example further? All the styling is not needed. And currently we can't run it without a database. Can you replace it with some simple dictionary lookup?
And what exactly is not working? What is happening? What did you expect?
These information would allow us to help more efficiently. Thanks!

@Smug246
Copy link
Contributor Author

Smug246 commented Aug 29, 2023

@falkoschindler So id expect it to just move on to the / page but it doesnt. check the edited code. it currently just sits on the same page wether the username, password are right or not

@falkoschindler
Copy link
Contributor

Strange. It works on my machine. After 1.5 seconds I'm redirected to "/".

@Smug246
Copy link
Contributor Author

Smug246 commented Aug 29, 2023

Strange. It works on my machine. After 1.5 seconds I'm redirected to "/".

are you definitley using the on air link thats generated. because its only on that link it doesnt work. it works normally without that

@falkoschindler
Copy link
Contributor

Oh, sorry, I forgot to go on air!
It might be related to #1501.
@rodja, can you confirm that?

@rodja
Copy link
Member

rodja commented Aug 29, 2023

@Smug246 oh yes. It is related to #1501 and #1517. It works again with NiceGUI 1.3.12 if you use RedirectResponse('/login'). I'll try to fix that later today for the upcoming 1.3.13. It should work with root / and without.

@Smug246
Copy link
Contributor Author

Smug246 commented Aug 29, 2023

uh ye i tried that with 1.3.12 and it still doesnt work.
image

@rodja
Copy link
Member

rodja commented Aug 29, 2023

What is the url if you see this error?

@Smug246
Copy link
Contributor Author

Smug246 commented Aug 29, 2023

@rodja
Copy link
Member

rodja commented Aug 29, 2023

That is odd. In #1501 I've added a middleware which should add the correct prefix. Are you sure this is the url you end up when starting you demo with 1.3.12 and open the On Air url? Would have been the expected result with 1.3.11. With 1.3.12 I get https://on-air.nicegui.io/devices/vk7MeJ7wlogin/ (eg. the / before login is missing).

@Smug246
Copy link
Contributor Author

Smug246 commented Aug 29, 2023

Desktop.2023.08.29.-.15.07.47.02.-.Trim.mp4

im on 1.3.12 as well:
image

@rodja
Copy link
Member

rodja commented Aug 29, 2023

Maybe you have a virtual env in the dir and the pip version is not used? Try

from nicegui import __version__
print(__version__)

@rodja rodja closed this as completed in ec77362 Aug 29, 2023
@falkoschindler falkoschindler added the bug Something isn't working label Aug 29, 2023
@falkoschindler falkoschindler added this to the 1.3.13 milestone Aug 29, 2023
@Smug246
Copy link
Contributor Author

Smug246 commented Aug 29, 2023

Maybe you have a virtual env in the dir and the pip version is not used? Try

from nicegui import __version__
print(__version__)

image

@rodja
Copy link
Member

rodja commented Aug 29, 2023

Wow. What a mystery. I'm not very confident that my bugfix ec77362 will solve it for you. But it is definitely fixing the original problem which I could reproduce. We will release 1.3.13 shortly. Let's try that and hope for the best.

@Smug246
Copy link
Contributor Author

Smug246 commented Aug 29, 2023

thanks for trying, i hope 1.3.13 works

@rodja
Copy link
Member

rodja commented Aug 29, 2023

Ok, 1.3.13 is just released. I hope it helps.

@rodja
Copy link
Member

rodja commented Sep 4, 2023

@Smug246 have you found the time to try this with 1.3.13?

@Smug246
Copy link
Contributor Author

Smug246 commented Sep 4, 2023

i'll test today when i'm home and let you know

@Smug246
Copy link
Contributor Author

Smug246 commented Sep 10, 2023

hi @rodja im still having problems after testing today. but its slightly different. when using on_air everytime a login is successful it will write authenticated true in the .nicegui directory, however on_air doesnt seem to track that storage so everytime i login a new session is made. and i can never login without going back to the login page.

from nicegui import ui, app
import asyncio
from fastapi.responses import RedirectResponse

@ui.page('/')
def home():
    if not app.storage.user.get('authenticated', False):
        return RedirectResponse('/login')

@ui.page('/')
def login():
    async def try_login():
        try:
            input_password = password.value

            actual_username = 'admin'
            actual_password = 'admin'

            if input_password == actual_password:
                app.storage.user.update({'username': actual_username, 'authenticated': True})
                ui.notify('Login Successful', color='positive')
                await asyncio.sleep(1.5)
                ui.open('/')
            else:
                ui.notify('Invalid username or password', color='negative')
                username.set_value('')
                password.set_value('')

        except Exception as e:
            print(e)

    ui.query('body')
    with ui.card().style('width: 250px;').classes('absolute-center'):
        username = ui.input('Enter Your Username').style('width: 225px;')
        password = ui.input('Enter Your Password', password=True, password_toggle_button=True).on('keydown.enter', try_login)
        ui.button('Log In', on_click=try_login)

ui.run(on_air=True, reload=False, storage_secret='secret')

EDIT: i've just deployed my site using docker and fly.io and the same problem occurs but with just:

ui.run(title='Freight Forever (Beta)', storage_secret='Freight_Forever_Secret', reload=False)

@rodja rodja changed the title authentication issues with on_air authentication issues with on_air (RedirectResponse not working properly) Sep 11, 2023
@rodja
Copy link
Member

rodja commented Sep 11, 2023

That looks like a completely other issue that the non-working RedirectResponse. Could you repost this a a new issue?
If you deploy to fly.io make sure you have only one instance or sync the .nicegui between instances or use app.storage.browser. Otherwise each instance has a different set of storage informations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants