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

refactor: code base improvements #959

Merged
merged 13 commits into from
Jan 1, 2021
Merged
2 changes: 1 addition & 1 deletion src/adapters/fauna/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ const Adapter = (config, options = {}) => {
_debug('getSession', sessionToken)

try {
var session = await faunaClient.query(
const session = await faunaClient.query(
q.Get(
q.Match(
q.Index('session_by_token'),
Expand Down
18 changes: 7 additions & 11 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/* global fetch:false */
import { useState, useEffect, useContext, createContext, createElement } from 'react'
import logger from '../lib/logger'
import parseUrl from '../lib/parse-url'
import baseUrl from '../lib/baseUrl'

// This behaviour mirrors the default behaviour for getting the site name that
// happens server side in server/index.js
Expand All @@ -22,8 +22,7 @@ import parseUrl from '../lib/parse-url'
// 2. When invoked server side the value is picked up from an environment
// variable and defaults to 'http://localhost:3000'.
const __NEXTAUTH = {
baseUrl: parseUrl(process.env.NEXTAUTH_URL || process.env.VERCEL_URL).baseUrl,
basePath: parseUrl(process.env.NEXTAUTH_URL).basePath,
baseUrl: baseUrl(),
keepAlive: 0, // 0 == disabled (don't send); 60 == send every 60 seconds
clientMaxAge: 0, // 0 == disabled (only use cache); 60 == sync if last checked > 60 seconds ago
// Properties starting with _ are used for tracking internal app state
Expand Down Expand Up @@ -77,13 +76,11 @@ if (typeof window !== 'undefined') {
// method is being left in as an alternative, that will be helpful if/when we
// expose a vanilla JavaScript version that doesn't depend on React.
const setOptions = ({
baseUrl,
basePath,
baseUrl: _baseUrl,
clientMaxAge,
keepAlive
} = {}) => {
if (baseUrl) { __NEXTAUTH.baseUrl = baseUrl }
if (basePath) { __NEXTAUTH.basePath = basePath }
if (baseUrl) { __NEXTAUTH.baseUrl = baseUrl(_baseUrl) }
if (clientMaxAge) { __NEXTAUTH.clientMaxAge = clientMaxAge }
if (keepAlive) {
__NEXTAUTH.keepAlive = keepAlive
Expand Down Expand Up @@ -306,11 +303,10 @@ const _apiBaseUrl = () => {
if (!process.env.NEXTAUTH_URL) { logger.warn('NEXTAUTH_URL', 'NEXTAUTH_URL environment variable not set') }

// Return absolute path when called server side
return `${__NEXTAUTH.baseUrl}${__NEXTAUTH.basePath}`
} else {
// Return relative path when called client side
return __NEXTAUTH.basePath
return __NEXTAUTH.baseUrl.href
}
// Return relative path when called client side
return __NEXTAUTH.baseUrl.pathname
}

const _encodedForm = (formData) => {
Expand Down
30 changes: 30 additions & 0 deletions src/lib/baseUrl.js
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 === '/') {
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
parsedUrl.pathname = '/api/auth'
}
parsedUrl.pathname = parsedUrl.pathname.replace(/\/$/, '')
parsedUrl.href = parsedUrl.href.replace(/\/$/, '')
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved

return parsedUrl
} catch (error) {
logger.error('INVALID_URL', _url, error)
return new URL(defaultUrl)
}
}

export default baseUrl
2 changes: 1 addition & 1 deletion src/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CreateUserError extends UnknownError {
}

// Thrown when an Email address is already associated with an account
// but the user is trying an oAuth account that is not linked to it.
// but the user is trying an OAuth account that is not linked to it.
class AccountNotLinkedError extends UnknownError {
constructor (message) {
super(message)
Expand Down
27 changes: 0 additions & 27 deletions src/lib/parse-url.js

This file was deleted.

Loading