Remove lib docCookies

This commit is contained in:
printempw 2018-02-16 19:54:07 +08:00
parent a1cbac0afb
commit 3ebc206e7d
8 changed files with 27 additions and 157 deletions

View File

@ -24,7 +24,6 @@ module.exports = {
"blessing": true,
"debounce": false,
"showModal": false,
"docCookies": false,
"showAjaxError": false,
"getQueryString": false,
"TexturePreview": false

View File

@ -136,7 +136,12 @@ class UserController extends Controller
if ($this->user->changePasswd($request->input('new_password'))) {
event(new UserProfileUpdated($action, $this->user));
return json(trans('user.profile.password.success'), 0);
session()->flush();
return json(trans('user.profile.password.success'), 0)
->withCookie(cookie()->forget('uid'))
->withCookie(cookie()->forget('token'));
}
break; // @codeCoverageIgnore
@ -156,7 +161,10 @@ class UserController extends Controller
if ($this->user->setEmail($request->input('new_email'))) {
event(new UserProfileUpdated($action, $this->user));
return json(trans('user.profile.email.success'), 0);
return json(trans('user.profile.email.success'), 0)
->withCookie(cookie()->forget('uid'))
->withCookie(cookie()->forget('token'));
}
break; // @codeCoverageIgnore

View File

@ -1125,10 +1125,8 @@ describe('tests for "common" module', () => {
it('send feedbacks', async () => {
const fetch = jest.fn()
.mockReturnValue(Promise.resolve({ errno: 0, msg: 'Recorded.' }));
const docCookies = require('../common/cookie');
window.document.cookie = '';
window.docCookies = docCookies;
window.fetch = fetch;
window.blessing = {
site_name: 'inm',

View File

@ -3,43 +3,6 @@
const $ = require('jquery');
window.jQuery = window.$ = $;
describe('tests for "cookie" module', () => {
it('operates cookies', () => {
const cookies = require('../common/cookie');
expect(cookies.hasItem('key1')).toBe(false);
expect(cookies.getItem('key1')).toBeNull();
expect(cookies.setItem('key1', 'value1')).toBe(true);
expect(document.cookie).toBe('key1=value1');
expect(cookies.setItem('key2', 'value2')).toBe(true);
expect(document.cookie).toBe('key1=value1; key2=value2');
expect(cookies.hasItem('key1')).toBe(true);
expect(cookies.getItem('key1')).toBe('value1');
expect(cookies.hasItem('key2')).toBe(true);
expect(cookies.getItem('key2')).toBe('value2');
expect(cookies.keys()).toEqual(['key1', 'key2']);
expect(cookies.setItem('domain', 'value')).toBe(false);
expect(cookies.removeItem('key0')).toBe(false);
expect(cookies.removeItem('key2')).toBe(true);
expect(cookies.hasItem('key2')).toBe(false);
expect(document.cookie).toBe('key1=value1');
expect(cookies.removeItem('key1')).toBe(true);
expect(cookies.setItem('key3', 'value3', 50));
expect(cookies.getItem('key3')).toBe('value3');
expect(cookies.setItem('key3', 'value3', Infinity));
expect(cookies.getItem('key3')).toBe('value3');
expect(cookies.setItem('key3', 'value3', '60'));
expect(cookies.getItem('key3')).toBe('value3');
expect(cookies.setItem('key4', 'value3', new Date));
expect(cookies.removeItem('key3')).toBe(true);
expect(document.cookie).toBe('');
});
});
describe('tests for "i18n" module', () => {
const modulePath = '../common/i18n';

View File

@ -850,7 +850,6 @@ describe('tests for "profile" module', () => {
it('change password', async () => {
const fetch = jest.fn()
.mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' }))
.mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' }))
.mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' }))
.mockReturnValueOnce(Promise.reject());
@ -862,19 +861,12 @@ describe('tests for "profile" module', () => {
warning: jest.fn()
};
const showAjaxError = jest.fn();
const docCookies = {
removeItem: jest.fn()
};
window.fetch = fetch;
window.swal = swal;
window.trans = trans;
window.url = url;
window.toastr = toastr;
window.showAjaxError = showAjaxError;
window.logout = jest.fn()
.mockReturnValueOnce(Promise.resolve({ errno: 0 }))
.mockReturnValueOnce(Promise.reject());
window.docCookies = docCookies;
document.body.innerHTML = `
<input id="password" />
@ -911,10 +903,6 @@ describe('tests for "profile" module', () => {
dataType: 'json',
data: { current_password: 'password', new_password: 'new-password' }
});
expect(logout).toBeCalled();
await changePassword();
expect(docCookies.removeItem).toBeCalledWith('token');
await changePassword();
expect(swal).toBeCalledWith({ type: 'warning', text: 'warning' });
@ -925,9 +913,8 @@ describe('tests for "profile" module', () => {
it('change email', async () => {
const fetch = jest.fn()
.mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' }))
.mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' }))
.mockReturnValueOnce(Promise.resolve({ errno: 1, msg: 'warning' }))
.mockReturnValueOnce(Promise.resolve({ errno: 0, msg: 'success' }))
.mockReturnValueOnce(Promise.reject());
const swal = jest.fn()
.mockReturnValueOnce(Promise.resolve())
@ -941,19 +928,12 @@ describe('tests for "profile" module', () => {
warning: jest.fn()
};
const showAjaxError = jest.fn();
const docCookies = {
removeItem: jest.fn()
};
window.fetch = fetch;
window.swal = swal;
window.trans = trans;
window.url = url;
window.toastr = toastr;
window.showAjaxError = showAjaxError;
window.logout = jest.fn()
.mockReturnValueOnce(Promise.resolve({ errno: 0 }))
.mockReturnValueOnce(Promise.reject());
window.docCookies = docCookies;
document.body.innerHTML = `
<input id="new-email" />
@ -985,15 +965,11 @@ describe('tests for "profile" module', () => {
dataType: 'json',
data: { new_email: 'a@b.c', password: 'pwd' }
});
expect(swal).toBeCalledWith({ type: 'success', text: 'success' });
expect(logout).toBeCalled();
await changeEmail();
expect(docCookies.removeItem).toBeCalled();
await changeEmail();
expect(swal).toBeCalledWith({ type: 'warning', text: 'warning' });
await changeEmail();
expect(swal).toBeCalledWith({ type: 'success', text: 'success' });
await changeEmail();
expect(showAjaxError).toBeCalled();
});

View File

@ -26,8 +26,9 @@ function initTables() {
}
async function sendFeedback() {
if (docCookies.getItem('feedback_sent') !== null)
if (document.cookie.replace(/(?:(?:^|.*;\s*)feedback_sent\s*=\s*([^;]*).*$)|^.*$/, '$1') !== '') {
return;
}
try {
const { errno } = await fetch({
@ -41,8 +42,8 @@ async function sendFeedback() {
}
});
if (errno === 0) {
// Will be expired when current session ends
docCookies.setItem('feedback_sent', Date.now());
// It will be expired when current session ends
document.cookie = 'feedback_sent=' + Date.now();
console.log('Feedback sent. Thank you!');
}

View File

@ -1,64 +0,0 @@
/* eslint-disable */
/*\
|*|
|*| :: cookies.js ::
|*|
|*| A complete cookies reader/writer framework with full unicode support.
|*|
|*| https://developer.mozilla.org/en-US/docs/DOM/document.cookie
|*|
|*| This framework is released under the GNU Public License, version 3 or later.
|*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*| Syntaxes:
|*|
|*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*| * docCookies.getItem(name)
|*| * docCookies.removeItem(name[, path], domain)
|*| * docCookies.hasItem(name)
|*| * docCookies.keys()
|*|
\*/
var docCookies = {
getItem: function (sKey) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
return true;
},
removeItem: function (sKey, sPath, sDomain) {
if (!sKey || !this.hasItem(sKey)) { return false; }
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function (sKey) {
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: /* optional method: you can safely remove it! */ function () {
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
return aKeys;
}
};
if (process.env.NODE_ENV === 'test') {
module.exports = docCookies;
}

View File

@ -70,18 +70,13 @@ async function changePassword() {
});
if (errno === 0) {
try {
await swal({
type: 'success',
text: msg
});
await logout();
} catch (error) {
docCookies.removeItem('token') && console.warn(error);
} finally {
window.location = url('auth/login');
}
return;
await swal({
type: 'success',
text: msg
});
// Cookies were already deleted by remote server
return window.location = url('auth/login');
} else {
return swal({ type: 'warning', text: msg });
}
@ -136,14 +131,8 @@ async function changeEmail() {
type: 'success',
text: msg
});
try {
await logout();
} catch (error) {
docCookies.removeItem('token') && console.warn(error);
} finally {
window.location = url('auth/login');
}
return window.location = url('auth/login');
} else {
return swal({ type: 'warning', text: msg });
}