Skip to content

Commit

Permalink
Merge pull request #957 from appwrite/fix-web-redirect
Browse files Browse the repository at this point in the history
fix(web): reverting to previous webauth redirect logic
  • Loading branch information
TorstenDittmann authored Aug 26, 2024
2 parents 8835175 + 94788f1 commit 0cc8391
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
21 changes: 3 additions & 18 deletions templates/web/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,10 @@ class Client {

/**
* Subscribes to {{spec.title | caseUcfirst}} events and passes you the payload in realtime.
*
* @param {string|string[]} channels
*
* @param {string|string[]} channels
* Channel to subscribe - pass a single channel as a string or multiple with an array of strings.
*
*
* Possible channels are:
* - account
* - collections
Expand Down Expand Up @@ -633,21 +633,6 @@ class Client {
return response;
}

async redirect(method: string, url: URL, headers: Headers = {}, params: Payload = {}): Promise<string> {
const { uri, options } = this.prepareRequest(method, url, headers, params);

const response = await fetch(uri, {
...options,
redirect: 'manual'
});

if (response.status !== 301 && response.status !== 302) {
throw new {{spec.title | caseUcfirst}}Exception('Invalid redirect', response.status);
}

return response.headers.get('location') || '';
}

async call(method: string, url: URL, headers: Headers = {}, params: Payload = {}, responseType = 'json'): Promise<any> {
const { uri, options } = this.prepareRequest(method, url, headers, params);

Expand Down
28 changes: 19 additions & 9 deletions templates/web/src/services/template.ts.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Service } from '../service';
import { {{ spec.title | caseUcfirst}}Exception, Client, type Payload, UploadProgress } from '../client';
import type { Models } from '../models';
{% set added = [] %}
Expand Down Expand Up @@ -69,17 +70,26 @@ export class {{ service.name | caseUcfirst }} {
{%~ endfor %}
}

{%~ if method.type == 'location' or method.type == 'webAuth' %}
{%~ if method.auth|length > 0 %}
{%~ for node in method.auth %}
{%~ for key,header in node|keys %}
payload['{{header|caseLower}}'] = this.client.config.{{header|caseLower}};
{%~ endfor %}
{%~ endfor %}
{%~ endif %}
for (const [key, value] of Object.entries(Service.flatten(payload))) {
uri.searchParams.append(key, value);
}
{%~ endif %}

{%~ if method.type == 'webAuth' %}
const location = await this.client.redirect(
'{{ method.method | caseLower }}',
uri,
apiHeaders,
payload
);
if (typeof window !== 'undefined') {
window.location.href = location;
if (typeof window !== 'undefined' && window?.location) {
window.location.href = uri.toString();
return;
} else {
return uri.toString();
}
return location.toString();
{%~ elseif method.type == 'location' %}
{%~ for node in method.auth %}
{%~ for key, header in node|keys %}
Expand Down

0 comments on commit 0cc8391

Please sign in to comment.