Skip to content

Commit a206c06

Browse files
Merge pull request #570 from AnswerDotAI/handle-oauth-error
handle oauth error
2 parents ef8f676 + f018749 commit a206c06

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

fasthtml/oauth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ def url_match(url, patterns=http_patterns):
138138

139139
# %% ../nbs/api/08_oauth.ipynb
140140
class OAuth:
141-
def __init__(self, app, cli, skip=None, redir_path='/redirect', logout_path='/logout', login_path='/login', https=True, http_patterns=http_patterns):
142-
if not skip: skip = [redir_path,login_path]
141+
def __init__(self, app, cli, skip=None, redir_path='/redirect', error_path='/error', logout_path='/logout', login_path='/login', https=True, http_patterns=http_patterns):
142+
if not skip: skip = [redir_path,error_path,login_path]
143143
store_attr()
144144
def before(req, session):
145145
auth = req.scope['auth'] = session.get('auth')
@@ -150,8 +150,8 @@ def before(req, session):
150150
app.before.append(Beforeware(before, skip=skip))
151151

152152
@app.get(redir_path)
153-
def redirect(code:str, req, session, state:str=None):
154-
if not code: return "No code provided!"
153+
def redirect(req, session, code:str=None, error:str=None, state:str=None):
154+
if not code: session['oauth_error']=error; return RedirectResponse(self.error_path, status_code=303)
155155
scheme = 'http' if url_match(req.url,self.http_patterns) or not self.https else 'https'
156156
base_url = f"{scheme}://{req.url.netloc}"
157157
info = AttrDictDefault(cli.retr_info(code, base_url+redir_path))

nbs/api/08_oauth.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@
417417
"source": [
418418
"#| export\n",
419419
"class OAuth:\n",
420-
" def __init__(self, app, cli, skip=None, redir_path='/redirect', logout_path='/logout', login_path='/login', https=True, http_patterns=http_patterns):\n",
421-
" if not skip: skip = [redir_path,login_path]\n",
420+
" def __init__(self, app, cli, skip=None, redir_path='/redirect', error_path='/error', logout_path='/logout', login_path='/login', https=True, http_patterns=http_patterns):\n",
421+
" if not skip: skip = [redir_path,error_path,login_path]\n",
422422
" store_attr()\n",
423423
" def before(req, session):\n",
424424
" auth = req.scope['auth'] = session.get('auth')\n",
@@ -429,8 +429,8 @@
429429
" app.before.append(Beforeware(before, skip=skip))\n",
430430
"\n",
431431
" @app.get(redir_path)\n",
432-
" def redirect(code:str, req, session, state:str=None):\n",
433-
" if not code: return \"No code provided!\"\n",
432+
" def redirect(req, session, code:str=None, error:str=None, state:str=None):\n",
433+
" if not code: session['oauth_error']=error; return RedirectResponse(self.error_path, status_code=303)\n",
434434
" scheme = 'http' if url_match(req.url,self.http_patterns) or not self.https else 'https'\n",
435435
" base_url = f\"{scheme}://{req.url.netloc}\"\n",
436436
" info = AttrDictDefault(cli.retr_info(code, base_url+redir_path))\n",

0 commit comments

Comments
 (0)