Skip to content

Commit

Permalink
fix(cms): fix password updated on forms when untouched (#79)
Browse files Browse the repository at this point in the history
when a resource is being updated on a form on the cms, if the field is untouched, the cms updates
the database value nonetheless. this fix makes sure if a field is untouched, its database value is
also untouched

fix #78
  • Loading branch information
bahdcoder authored Mar 2, 2021
1 parent a1cd9d4 commit 365e6af
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 195 deletions.
122 changes: 65 additions & 57 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
hasMany,
boolean,
select,
password,
graphQlQuery,
GraphQLPluginContext,
route,
Expand Down Expand Up @@ -83,7 +84,7 @@ class Auth {
verifyEmails: false,
skipWelcomeEmail: false,
rolesAndPermissions: false,
providers: {},
providers: {}
}

private TwoFactorAuth: any = null
Expand Down Expand Up @@ -230,7 +231,7 @@ class Auth {
}

private userResource() {
let passwordField = text('Password')
let passwordField = password('Password')

let socialFields: FieldContract[] = []

Expand Down Expand Up @@ -259,8 +260,7 @@ class Auth {
})
.creationRules('required')
.onlyOnForms()
.hideOnUpdateApi()
.hideOnUpdate(),
.hideOnUpdateApi(),
boolean('Blocked')
.nullable()
.default(false)
Expand Down Expand Up @@ -567,7 +567,8 @@ class Auth {
authConfig: this.config,
resourcesMap: this.resources,
apiPath: this.config.apiPath,
getUserPayloadFromProviderData: this.config.getUserPayloadFromProviderData
getUserPayloadFromProviderData: this.config
.getUserPayloadFromProviderData
})
}

Expand Down Expand Up @@ -1066,60 +1067,65 @@ class Auth {
]
: []),
...(this.socialAuthEnabled()
? this.config.separateSocialLoginAndRegister ? [
route(`Social Auth Login`)
.path(this.getApiPath('social/login'))
.post()
.id('social_login')
.extend({
docs: {
...extend,
summary: `Login a ${name} via a social provider.`,
description: `This operation requires an access_token gotten after a redirect from the social provider.`
}
})
.handle(async (request, response) =>
response.formatter.ok(
await this.socialAuth(request as any, 'login')
? this.config.separateSocialLoginAndRegister
? [
route(`Social Auth Login`)
.path(this.getApiPath('social/login'))
.post()
.id('social_login')
.extend({
docs: {
...extend,
summary: `Login a ${name} via a social provider.`,
description: `This operation requires an access_token gotten after a redirect from the social provider.`
}
})
.handle(async (request, response) =>
response.formatter.ok(
await this.socialAuth(
request as any,
'login'
)
)
),
route(`Social Auth Register`)
.path(this.getApiPath('social/register'))
.id('social_register')
.post()
.extend({
docs: {
...extend,
summary: `Register a ${name} via a social provider.`,
description: `This operation requires an access_token gotten after a redirect from the social provider.`
}
})
.handle(async (request, response) =>
response.formatter.ok(
await this.socialAuth(
request as any,
'register'
)
)
)
),
route(`Social Auth Register`)
.path(this.getApiPath('social/register'))
.id('social_register')
.post()
.extend({
docs: {
...extend,
summary: `Register a ${name} via a social provider.`,
description: `This operation requires an access_token gotten after a redirect from the social provider.`
}
})
.handle(async (request, response) =>
response.formatter.ok(
await this.socialAuth(
request as any,
'register'
]
: [
route(`Social Auth Confirm`)
.path(this.getApiPath(`social/confirm`))
.id('social_confirm')
.post()
.extend({
docs: {
...extend,
summary: `Confirm a ${name} (login or register) via a social provider.`,
description: `This operation requires an access_token gotten after a redirect from the social provider.`
}
})
.handle(async (request, response) =>
response.formatter.ok(
await this.socialAuth(request as any)
)
)
)
] : [
route(`Social Auth Confirm`)
.path(this.getApiPath(`social/confirm`))
.id('social_confirm')
.post()
.extend({
docs: {
...extend,
summary: `Confirm a ${name} (login or register) via a social provider.`,
description: `This operation requires an access_token gotten after a redirect from the social provider.`
}
})
.handle(async (request, response) => response.formatter.ok(
await this.socialAuth(
request as any,
)
))
]
]
: []),
...(this.config.enableRefreshTokens
? [
Expand Down Expand Up @@ -1815,7 +1821,9 @@ class Auth {
throw ctx.userInputError('Invalid email verification token.')
}

public getUserPayloadFromProviderData(getUserPayloadFromProviderData: AuthPluginConfig['getUserPayloadFromProviderData']) {
public getUserPayloadFromProviderData(
getUserPayloadFromProviderData: AuthPluginConfig['getUserPayloadFromProviderData']
) {
this.config.getUserPayloadFromProviderData = getUserPayloadFromProviderData

return this
Expand Down
1 change: 1 addition & 0 deletions packages/cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"build": "yarn build:server && cross-env NODE_ENV=production webpack --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"@tensei/core": "^0.7.16",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/plugin-proposal-object-rest-spread": "^7.12.1",
"@babel/preset-react": "^7.12.7",
Expand Down
19 changes: 10 additions & 9 deletions packages/cms/pages/CreateResource/CreateResource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ const CreateResource: React.FC<CreateResourceProps> = ({}) => {
return <Redirect to={window.Tensei.getPath('404')} />
}

const creationFields = resource.fields.filter(field => field.showOnCreation)
const isEditing = !!params.id

const sidebarFields = creationFields.filter(field => field.sidebar)
const mainbarFields = creationFields.filter(field => !field.sidebar)
const formFields = resource.fields.filter(field =>
isEditing ? field.showOnUpdate : field.showOnCreation
)

const sidebarVisible = sidebarFields.length > 0
const sidebarFields = formFields.filter(field => field.sidebar)
const mainbarFields = formFields.filter(field => !field.sidebar)

const isEditing = !!params.id
const sidebarVisible = sidebarFields.length > 0

const onErrorCatch = (error: AxiosError) => {
window.Tensei.error(`Failed saving ${resource.name.toLowerCase()}.`)
Expand Down Expand Up @@ -79,7 +81,7 @@ const CreateResource: React.FC<CreateResourceProps> = ({}) => {
if (!isEditing) {
let formData: AbstractData = {}

creationFields.forEach(field => {
formFields.forEach(field => {
formData[field.inputName] = field.defaultValue
})

Expand All @@ -94,9 +96,8 @@ const CreateResource: React.FC<CreateResourceProps> = ({}) => {
.then(({ data }) => {
let formData: AbstractData = {}

creationFields.forEach(field => {
formData[field.inputName] =
data.data[field.inputName] || field.defaultValue
formFields.forEach(field => {
formData[field.inputName] = data.data[field.inputName]
})

setForm(formData)
Expand Down
Loading

0 comments on commit 365e6af

Please sign in to comment.