Skip to content

Commit

Permalink
fix: csrf not set for egg-security
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcarl committed Mar 15, 2018
1 parent ac1b524 commit 7f68210
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
return this.renderVueClient(name, locals, options);
},
renderVueClient(name, locals, options = {}) {
locals = this.app.vue.normalizeLocals(locals);
locals = this.app.vue.normalizeLocals(this, locals);
return this.app.vue.renderClient(name, locals, options).then(html => {
this.body = html;
});
Expand Down
4 changes: 2 additions & 2 deletions lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class Engine {
return this[VUE_RESOURCE];
}

normalizeLocals(locals = {}) {
normalizeLocals(ctx, locals = {}) {
[ 'ctx', 'request', 'helper' ].forEach(key => {
Object.defineProperty(locals, key, { enumerable: false });
});
return locals;
return Object.assign({}, { csrf: ctx.csrf }, locals);
}
createBundleRenderer(name, renderOptions) {
if (this.bundleCache) {
Expand Down
5 changes: 3 additions & 2 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class View {
constructor(ctx) {
this.ctx = ctx;
this.app = ctx.app;
this.config = this.app.config.vuessr;
}
Expand All @@ -15,7 +16,7 @@ class View {
* @return {Object} Promise
*/
render(name, locals, options = {}) {
locals = this.app.vue.normalizeLocals(locals);
locals = this.app.vue.normalizeLocals(this.ctx, locals);
const context = { state: locals };
return this.app.vue.render(name, context, options).then(html => {
return this.app.vue.resource.inject(html, options.name, context, options);
Expand All @@ -29,7 +30,7 @@ class View {
}

renderString(tpl, locals) {
locals = this.app.vue.normalizeLocals(locals);
locals = this.app.vue.normalizeLocals(this.ctx, locals);
return this.app.vue.renderString(tpl, locals);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-view-vue-ssr",
"version": "3.0.7",
"version": "3.0.8",
"description": "vue server side render solution for egg",
"eggPlugin": {
"name": "vuessr"
Expand Down
3 changes: 3 additions & 0 deletions test/view-vue-ssr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('test/view-vue-ssr.test.js', () => {
.get('/render')
.expect(200)
.expect(res => {
assert(res.text.indexOf('"csrf"') > -1);
assert(res.text.indexOf('data-server-rendered="true"') > -1);
assert(res.text.indexOf('</body></html>') > -1);
assert(res.text.indexOf('vue server side render!') > -1);
Expand All @@ -50,6 +51,7 @@ describe('test/view-vue-ssr.test.js', () => {
.get('/renderClient')
.expect(200)
.expect(res => {
assert(res.text.indexOf('"csrf"') > -1);
assert(res.text.indexOf('data-server-rendered="true"') > -1);
assert(res.text.indexOf('name="client"') > -1);
assert(res.text.indexOf('vue server side render!') > -1);
Expand All @@ -65,6 +67,7 @@ describe('test/view-vue-ssr.test.js', () => {
.get('/renderVueClient')
.expect(200)
.expect(res => {
assert(res.text.indexOf('"csrf"') > -1);
assert(res.text.indexOf('data-server-rendered="true"') > -1);
assert(res.text.indexOf('name="client"') > -1);
assert(res.text.indexOf('vue server side render!') > -1);
Expand Down

0 comments on commit 7f68210

Please sign in to comment.