-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use native URL instead of string concats
- Loading branch information
1 parent
d04185c
commit 690c55b
Showing
12 changed files
with
108 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import logger from './logger' | ||
|
||
/** | ||
* Simple universal (client/server) function to split host and path. | ||
* It can also take a url (either URL or a string) and parses it correctly. | ||
* @returns {URL} | ||
*/ | ||
function baseUrl (url) { | ||
let _url = url || process.env.NEXTAUTH_URL || process.env.VERCEL_URL | ||
if (typeof _url !== 'string' && !(_url instanceof URL)) { | ||
throw new Error('baseUrl must be either a valid URL object or a valid string URL') | ||
} | ||
const defaultUrl = 'http://localhost:3000/api/auth' | ||
_url = _url || defaultUrl | ||
try { | ||
const parsedUrl = new URL(_url) | ||
if (parsedUrl.pathname === '/') { | ||
parsedUrl.pathname = '/api/auth' | ||
} | ||
parsedUrl.pathname = parsedUrl.pathname.replace(/\/$/, '') | ||
parsedUrl.href = parsedUrl.href.replace(/\/$/, '') | ||
|
||
return parsedUrl | ||
} catch (error) { | ||
logger.error('INVALID_URL', _url, error) | ||
return new URL(defaultUrl) | ||
} | ||
} | ||
|
||
export default baseUrl |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { h } from 'preact' // eslint-disable-line no-unused-vars | ||
import render from 'preact-render-to-string' | ||
import baseUrl from '../../lib/baseUrl' | ||
|
||
export default ({ baseUrl }) => { | ||
export default function verifyRequest () { | ||
return render( | ||
<div className='verify-request'> | ||
<h1>Check your email</h1> | ||
<p>A sign in link has been sent to your email address.</p> | ||
<p><a className='site' href={baseUrl}>{baseUrl.replace(/^https?:\/\//, '')}</a></p> | ||
<p><a className='site' href={baseUrl().origin}>{baseUrl().host}</a></p> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.