Skip to content

Commit

Permalink
Headers already sent temp workaround
Browse files Browse the repository at this point in the history
- This round we will be using the beta tag in case I need to do any drastic changes.
  • Loading branch information
Denoder committed Nov 17, 2023
1 parent b2785e3 commit fa55a6d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nuxt-alt/auth",
"version": "2.7.5",
"version": "2.7.7-beta.0",
"description": "An alternative module to @nuxtjs/auth",
"homepage": "https://github.com/nuxt-alt/auth",
"author": "Denoder",
Expand Down
16 changes: 8 additions & 8 deletions src/runtime/core/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class Auth {
}

if (!this.ctx.$http) {
return Promise.reject(new Error('[AUTH] add the @nuxtjs-alt/http module to nuxt.config file'));
return Promise.reject(new Error('[AUTH] add the @nuxt-alt/http module to nuxt.config file'));
}

return this.ctx.$http.raw(request).catch((error: Error) => {
Expand Down Expand Up @@ -350,22 +350,22 @@ export class Auth {
* @returns
*/
redirect(name: string, route: Route | false = false, router: boolean = true) {
const currentRoute = useRoute();
const currentRouter = useRouter();

if (!this.options.redirect) {
return;
}

const nuxtRoute = this.options.fullPathRedirect ? currentRoute.fullPath : currentRoute.path
const from = route ? (this.options.fullPathRedirect ? route.fullPath : route.path) : nuxtRoute;

let to: string = this.options.redirect[name as keyof typeof this.options.redirect];

if (!to) {
return;
}

const currentRoute = useRoute();
const currentRouter = useRouter();

const nuxtRoute = this.options.fullPathRedirect ? currentRoute.fullPath : currentRoute.path
const from = route ? (this.options.fullPathRedirect ? route.fullPath : route.path) : nuxtRoute;

const queryReturnTo = currentRoute.query.to;

// Apply rewrites
Expand Down Expand Up @@ -407,7 +407,7 @@ export class Auth {
}
}

if (!router || !isRelativeURL(to)) {
if (process.client && (!router || !isRelativeURL(to))) {
window.location.replace(to);
}
else {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/schemes/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export class LocalScheme<OptionsT extends LocalSchemeOptions = LocalSchemeOption
}

this.$auth.setUser(userData);

return response;
})
.catch((error) => {
Expand Down
12 changes: 6 additions & 6 deletions src/types/scheme.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HTTPRequest, HTTPResponse } from '.';
import type { Auth } from '../runtime/core';
import type { Token, IdToken, RefreshToken, RefreshController, RequestHandler,} from '../runtime/inc';
import type { Token, IdToken, RefreshToken, RefreshController, RequestHandler } from '../runtime/inc';
import type { PartialExcept } from './utils';

export interface UserOptions {
Expand Down Expand Up @@ -37,14 +37,14 @@ export interface Scheme<OptionsT extends SchemeOptions = SchemeOptions> {
options: OptionsT;
name?: string;
$auth: Auth;
mounted?(...args: any[]): Promise<HTTPResponse | void>;
mounted?(...args: any[]): Promise<HTTPResponse<any> | void>;
check?(checkStatus: boolean): SchemeCheck;
login(...args: any[]): Promise<HTTPResponse | void>;
fetchUser(endpoint?: HTTPRequest): Promise<HTTPResponse | void>;
login(...args: any[]): Promise<HTTPResponse<any> | void>;
fetchUser(endpoint?: HTTPRequest): Promise<HTTPResponse<any> | void>;
setUserToken?(
token: string | boolean,
refreshToken?: string | boolean
): Promise<HTTPResponse | void>;
): Promise<HTTPResponse<any> | void>;
logout?(endpoint?: HTTPRequest): Promise<void> | void;
reset?(options?: { resetInterceptor: boolean }): void;
}
Expand Down Expand Up @@ -103,5 +103,5 @@ export interface RefreshableSchemeOptions extends TokenableSchemeOptions {
export interface RefreshableScheme<OptionsT extends RefreshableSchemeOptions = RefreshableSchemeOptions> extends TokenableScheme<OptionsT> {
refreshToken: RefreshToken;
refreshController: RefreshController;
refreshTokens(): Promise<HTTPResponse | void>;
refreshTokens(): Promise<HTTPResponse<any> | void>;
}
12 changes: 7 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,13 @@ export function setH3Cookie(event: H3Event, serializedCookie: string) {
if (!Array.isArray(cookies)) cookies = [cookies];
cookies.unshift(serializedCookie);

event.node.res.setHeader('Set-Cookie', cookies.filter(
(value, index, items) => items.findIndex(
(val) => val.startsWith(value.slice(0, value.indexOf('=')))
) === index
));
if (!event.node.res.headersSent) {
event.node.res.setHeader('Set-Cookie', cookies.filter(
(value, index, items) => items.findIndex(
(val) => val.startsWith(value.slice(0, value.indexOf('=')))
) === index
));
}
}

export const hasOwn = <O extends object, K extends PropertyKey>(object: O, key: K) => Object.hasOwn ? Object.hasOwn(object, key) : Object.prototype.hasOwnProperty.call(object, key);

0 comments on commit fa55a6d

Please sign in to comment.