How to add authentication to portal pages or web pages

How to add authentication to portal pages or web pages .
Is thare any inbuild functionality provided by frappe to add login_required to Portal page or Web Page.
I have a custom app and i need to add authentication to few of my web pages.
Can any one please guide me

In your get_context method you may add something like this:

if frappe.session.user=='Guest':
    frappe.throw(_("You need to be logged in to access this page"), frappe.PermissionError)
2 Likes

As mincerray explained you can add the context script under the web page section as shown in the below image.

1 Like

thanks for quick reply

Thanks Aadhil it worked

Hey Aadhil, if I need to redirect to login page instead of showing permission error, is that possible? Tried few ways and is not working for me.

if frappe.session.user == ‘Guest’:
frappe.local.flags.redirect_location = ‘/login’
raise frappe.Redirect

Is there any way to protect a whole directory (Structure) with one python script?

has anyone tried to redirect the user to the original web page after the user has successfully logged in?