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

Set x_mtok Cookie on Cordova #676

Merged
merged 8 commits into from
Sep 29, 2019
28 changes: 10 additions & 18 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import { DDP } from 'meteor/ddp-client';
import { Cookies } from 'meteor/ostrio:cookies';
import { check, Match } from 'meteor/check';
import { UploadInstance } from './upload.js';
Expand Down Expand Up @@ -113,29 +114,20 @@ export class FilesCollection extends FilesCollectionCore {
}

const setTokenCookie = () => {
if ((!cookie.has('x_mtok') && Meteor.connection._lastSessionId) || (cookie.has('x_mtok') && (cookie.get('x_mtok') !== Meteor.connection._lastSessionId))) {
cookie.set('x_mtok', Meteor.connection._lastSessionId, {
path: '/'
});
}
};

const unsetTokenCookie = () => {
dr-dimitru marked this conversation as resolved.
Show resolved Hide resolved
if (cookie.has('x_mtok')) {
cookie.remove('x_mtok', '/');
if (Meteor.connection._lastSessionId) {
cookie.set('x_mtok', Meteor.connection._lastSessionId, { path: '/' });
if (Meteor.isCordova) {
cookie.send();
}
}
};

if (typeof Accounts !== 'undefined' && Accounts !== null) {
Meteor.startup(() => {
setTokenCookie();
});
Accounts.onLogin(() => {
setTokenCookie();
});
Accounts.onLogout(() => {
unsetTokenCookie();
DDP.onReconnect((conn) => {
conn.onReconnect = setTokenCookie;
});
Meteor.startup(setTokenCookie);
Accounts.onLogin(setTokenCookie);
}

check(this.onbeforeunloadMessage, Match.OneOf(String, Function));
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Npm.depends({
Package.onUse(function(api) {
api.versionsFrom('1.6.1');
api.use('webapp', 'server');
api.use(['reactive-var', 'tracker', 'http'], 'client');
api.use(['reactive-var', 'tracker', 'http', 'ddp-client'], 'client');
api.use(['mongo', 'check', 'random', 'ecmascript', 'ostrio:cookies@2.3.0'], ['client', 'server']);
api.addAssets('worker.min.js', 'client');
api.mainModule('server.js', 'server');
Expand Down
25 changes: 25 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import nodePath from 'path';
const bound = Meteor.bindEnvironment(callback => callback());
const NOOP = () => { };

const originRE = /^http:\/\/localhost:12\d\d\d$/;
s-ol marked this conversation as resolved.
Show resolved Hide resolved

/*
* @locus Anywhere
* @class FilesCollection
Expand Down Expand Up @@ -59,6 +61,7 @@ const NOOP = () => { };
* @param config.interceptDownload {Function} - [Server] Intercept download request, so you can serve file from third-party resource, arguments {http: {request: {...}, response: {...}}, fileRef: {...}}
* @param config.disableUpload {Boolean} - Disable file upload, useful for server only solutions
* @param config.disableDownload {Boolean} - Disable file download (serving), useful for file management only solutions
* @param config.disableCORS {Boolean} - [Server] Disable Cordova CORS access
s-ol marked this conversation as resolved.
Show resolved Hide resolved
* @param config._preCollection {Mongo.Collection} - [Server] Mongo preCollection Instance
* @param config._preCollectionName {String} - [Server] preCollection name
* @summary Create new instance of FilesCollection
Expand Down Expand Up @@ -90,6 +93,7 @@ export class FilesCollection extends FilesCollectionCore {
namingFunction: this.namingFunction,
responseHeaders: this.responseHeaders,
disableDownload: this.disableDownload,
disableCORS: this.disableCORS,
allowClientCode: this.allowClientCode,
downloadCallback: this.downloadCallback,
onInitiateUpload: this.onInitiateUpload,
Expand Down Expand Up @@ -205,6 +209,10 @@ export class FilesCollection extends FilesCollectionCore {
this.disableDownload = false;
}

if (!helpers.isBoolean(this.disableCORS)) {
this.disableCORS = false;
}

if (!helpers.isObject(this._currentUploads)) {
this._currentUploads = {};
}
Expand Down Expand Up @@ -435,6 +443,23 @@ export class FilesCollection extends FilesCollectionCore {
return;
}
WebApp.connectHandlers.use((httpReq, httpResp, next) => {
if (!this.disableCORS && !!~httpReq._parsedUrl.path.indexOf(`${this.downloadRoute}/`) && !httpResp.headersSent) {
if (originRE.test(httpReq.headers.origin)) {
httpResp.setHeader('Access-Control-Allow-Credentials', 'true');
httpResp.setHeader('Access-Control-Allow-Origin', httpReq.headers.origin);
dr-dimitru marked this conversation as resolved.
Show resolved Hide resolved
}

if (httpReq.method === 'OPTIONS') {
httpResp.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
httpResp.setHeader('Access-Control-Allow-Headers', 'Range, Content-Type, x-mtok, x-start, x-chunkid, x-fileid, x-eof');
s-ol marked this conversation as resolved.
Show resolved Hide resolved
httpResp.setHeader('Access-Control-Expose-Headers', 'Accept-Ranges, Content-Encoding, Content-Length, Content-Range');
dr-dimitru marked this conversation as resolved.
Show resolved Hide resolved
httpResp.setHeader('Allow', 'GET, POST, OPTIONS');
httpResp.writeHead(200);
httpResp.end();
return;
}
}

if (!this.disableUpload && !!~httpReq._parsedUrl.path.indexOf(`${this.downloadRoute}/${this.collectionName}/__upload`)) {
if (httpReq.method === 'POST') {
const handleError = (_error) => {
Expand Down