├── .editorconfig ├── .env.example ├── .eslintignore ├── .eslintrc ├── .gitignore ├── README.md ├── config ├── admin.js ├── api.js ├── database.js ├── middlewares.js └── server.js ├── favicon.ico ├── package.json ├── public ├── robots.txt └── uploads │ └── .gitkeep ├── src ├── admin │ ├── app.js │ └── webpack.config.js ├── api │ └── .gitkeep ├── extensions │ └── users-permissions │ │ └── strapi-server.js └── index.js └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{package.json,*.yml}] 12 | indent_style = space 13 | indent_size = 2 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | HOST=0.0.0.0 2 | PORT=1337 3 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | .cache 2 | build 3 | **/node_modules/** 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "extends": "eslint:recommended", 4 | "env": { 5 | "commonjs": true, 6 | "es6": true, 7 | "node": true, 8 | "browser": false 9 | }, 10 | "parserOptions": { 11 | "ecmaFeatures": { 12 | "experimentalObjectRestSpread": true, 13 | "jsx": false 14 | }, 15 | "sourceType": "module" 16 | }, 17 | "globals": { 18 | "strapi": true 19 | }, 20 | "rules": { 21 | "indent": ["error", 2, { "SwitchCase": 1 }], 22 | "linebreak-style": ["error", "unix"], 23 | "no-console": 0, 24 | "quotes": ["error", "single"], 25 | "semi": ["error", "always"] 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ############################ 2 | # OS X 3 | ############################ 4 | 5 | .DS_Store 6 | .AppleDouble 7 | .LSOverride 8 | Icon 9 | .Spotlight-V100 10 | .Trashes 11 | ._* 12 | 13 | 14 | ############################ 15 | # Linux 16 | ############################ 17 | 18 | *~ 19 | 20 | 21 | ############################ 22 | # Windows 23 | ############################ 24 | 25 | Thumbs.db 26 | ehthumbs.db 27 | Desktop.ini 28 | $RECYCLE.BIN/ 29 | *.cab 30 | *.msi 31 | *.msm 32 | *.msp 33 | 34 | 35 | ############################ 36 | # Packages 37 | ############################ 38 | 39 | *.7z 40 | *.csv 41 | *.dat 42 | *.dmg 43 | *.gz 44 | *.iso 45 | *.jar 46 | *.rar 47 | *.tar 48 | *.zip 49 | *.com 50 | *.class 51 | *.dll 52 | *.exe 53 | *.o 54 | *.seed 55 | *.so 56 | *.swo 57 | *.swp 58 | *.swn 59 | *.swm 60 | *.out 61 | *.pid 62 | 63 | 64 | ############################ 65 | # Logs and databases 66 | ############################ 67 | 68 | .tmp 69 | *.log 70 | *.sql 71 | *.sqlite 72 | *.sqlite3 73 | 74 | 75 | ############################ 76 | # Misc. 77 | ############################ 78 | 79 | *# 80 | ssl 81 | .idea 82 | nbproject 83 | public/uploads/* 84 | !public/uploads/.gitkeep 85 | 86 | ############################ 87 | # Node.js 88 | ############################ 89 | 90 | lib-cov 91 | lcov.info 92 | pids 93 | logs 94 | results 95 | node_modules 96 | .node_history 97 | 98 | ############################ 99 | # Tests 100 | ############################ 101 | 102 | testApp 103 | coverage 104 | 105 | ############################ 106 | # Strapi 107 | ############################ 108 | 109 | .env 110 | license.txt 111 | exports 112 | *.cache 113 | build 114 | .strapi-updater.json 115 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Strapi application 2 | 3 | A quick description of your strapi application 4 | -------------------------------------------------------------------------------- /config/admin.js: -------------------------------------------------------------------------------- 1 | module.exports = ({ env }) => ({ 2 | auth: { 3 | secret: env('ADMIN_JWT_SECRET', '5f88045fb6faac4d17549a2843844a4c'), 4 | }, 5 | }); 6 | -------------------------------------------------------------------------------- /config/api.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rest: { 3 | defaultLimit: 25, 4 | maxLimit: 100, 5 | withCount: true, 6 | }, 7 | }; 8 | -------------------------------------------------------------------------------- /config/database.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = ({ env }) => ({ 4 | connection: { 5 | client: 'sqlite', 6 | connection: { 7 | filename: path.join(__dirname, '..', env('DATABASE_FILENAME', '.tmp/data.db')), 8 | }, 9 | useNullAsDefault: true, 10 | }, 11 | }); 12 | -------------------------------------------------------------------------------- /config/middlewares.js: -------------------------------------------------------------------------------- 1 | module.exports = [ 2 | 'strapi::errors', 3 | 'strapi::security', 4 | 'strapi::cors', 5 | 'strapi::poweredBy', 6 | 'strapi::logger', 7 | 'strapi::query', 8 | 'strapi::body', 9 | 'strapi::favicon', 10 | 'strapi::public', 11 | ]; 12 | -------------------------------------------------------------------------------- /config/server.js: -------------------------------------------------------------------------------- 1 | module.exports = ({ env }) => ({ 2 | host: env('HOST', '0.0.0.0'), 3 | port: env.int('PORT', 1337), 4 | }); 5 | -------------------------------------------------------------------------------- /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strapi-support-demo-apps/strapi-example-v4-customUpdateMe/1ce370a618db7183b04e42860676958313c3cedd/favicon.ico -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "strapi-example-custom-update-me", 3 | "private": true, 4 | "version": "0.1.0", 5 | "description": "A Strapi application", 6 | "scripts": { 7 | "develop": "strapi develop", 8 | "start": "strapi start", 9 | "build": "strapi build", 10 | "strapi": "strapi" 11 | }, 12 | "devDependencies": {}, 13 | "dependencies": { 14 | "@strapi/strapi": "4.0.0", 15 | "@strapi/plugin-users-permissions": "4.0.0", 16 | "@strapi/plugin-i18n": "4.0.0", 17 | "sqlite3": "5.0.2" 18 | }, 19 | "author": { 20 | "name": "A Strapi developer" 21 | }, 22 | "strapi": { 23 | "uuid": "65b06099-16b6-4a5f-ac04-ed6c0ca8f79a" 24 | }, 25 | "engines": { 26 | "node": ">=12.x.x <=16.x.x", 27 | "npm": ">=6.0.0" 28 | }, 29 | "license": "MIT" 30 | } 31 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # To prevent search engines from seeing the site altogether, uncomment the next two lines: 2 | # User-Agent: * 3 | # Disallow: / 4 | -------------------------------------------------------------------------------- /public/uploads/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strapi-support-demo-apps/strapi-example-v4-customUpdateMe/1ce370a618db7183b04e42860676958313c3cedd/public/uploads/.gitkeep -------------------------------------------------------------------------------- /src/admin/app.js: -------------------------------------------------------------------------------- 1 | export default { 2 | config: { 3 | locales: [ 4 | // 'ar', 5 | 'fr', 6 | // 'cs', 7 | // 'de', 8 | // 'dk', 9 | // 'es', 10 | // 'he', 11 | // 'id', 12 | // 'it', 13 | // 'ja', 14 | // 'ko', 15 | // 'ms', 16 | // 'nl', 17 | // 'no', 18 | // 'pl', 19 | // 'pt-BR', 20 | // 'pt', 21 | // 'ru', 22 | // 'sk', 23 | // 'sv', 24 | // 'th', 25 | // 'tr', 26 | // 'uk', 27 | // 'vi', 28 | // 'zh-Hans', 29 | // 'zh', 30 | ], 31 | }, 32 | bootstrap() {}, 33 | }; 34 | -------------------------------------------------------------------------------- /src/admin/webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* eslint-disable no-unused-vars */ 4 | module.exports = (config, webpack) => { 5 | // Note: we provide webpack above so you should not `require` it 6 | // Perform customizations to webpack config 7 | // Important: return the modified config 8 | return config; 9 | }; 10 | -------------------------------------------------------------------------------- /src/api/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strapi-support-demo-apps/strapi-example-v4-customUpdateMe/1ce370a618db7183b04e42860676958313c3cedd/src/api/.gitkeep -------------------------------------------------------------------------------- /src/extensions/users-permissions/strapi-server.js: -------------------------------------------------------------------------------- 1 | const _ = require('lodash'); 2 | 3 | module.exports = (plugin) => { 4 | const getController = name => { 5 | return strapi.plugins['users-permissions'].controller(name); 6 | }; 7 | 8 | // Create the new controller 9 | plugin.controllers.user.updateMe = async (ctx) => { 10 | const user = ctx.state.user; 11 | 12 | // User has to be logged in to update themselves 13 | if (!user) { 14 | return ctx.unauthorized(); 15 | } 16 | 17 | // Pick only specific fields for security 18 | const newData = _.pick(ctx.request.body, ['email', 'username', 'password', 'confirmPassword']); 19 | 20 | // Make sure there is no duplicate user with the same username 21 | if (newData.username) { 22 | const userWithSameUsername = await strapi 23 | .query('plugin::users-permissions.user') 24 | .findOne({ where: { username: newData.username } }); 25 | 26 | if (userWithSameUsername && userWithSameUsername.id != user.id) { 27 | return ctx.badRequest('Username already taken'); 28 | } 29 | } 30 | 31 | // Make sure there is no duplicate user with the same email 32 | if (newData.email) { 33 | const userWithSameEmail = await strapi 34 | .query('plugin::users-permissions.user') 35 | .findOne({ where: { email: newData.email.toLowerCase() } }); 36 | 37 | if (userWithSameEmail && userWithSameEmail.id != user.id) { 38 | return ctx.badRequest('Email already taken'); 39 | } 40 | newData.email = newData.email.toLowerCase(); 41 | } 42 | 43 | // Check if user is changing password and make sure passwords match 44 | if (newData.password) { 45 | if (!newData.confirmPassword) { 46 | return ctx.badRequest('Missing password confirmation'); 47 | } else if (newData.password !== newData.confirmPassword) { 48 | return ctx.badRequest('Passwords don\'t match') 49 | } 50 | delete newData.confirmPassword 51 | } 52 | 53 | // Reconstruct context so we can pass to the controller 54 | ctx.request.body = newData 55 | ctx.params = { id: user.id } 56 | 57 | // Update the user and return the sanitized data 58 | return await getController('user').update(ctx) 59 | }; 60 | 61 | // Add the custom route 62 | plugin.routes['content-api'].routes.unshift({ 63 | method: 'PUT', 64 | path: '/users/me', 65 | handler: 'user.updateMe', 66 | config: { 67 | prefix: '' 68 | } 69 | }); 70 | 71 | return plugin; 72 | }; 73 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | /** 5 | * An asynchronous register function that runs before 6 | * your application is initialized. 7 | * 8 | * This gives you an opportunity to extend code. 9 | */ 10 | register(/*{ strapi }*/) {}, 11 | 12 | /** 13 | * An asynchronous bootstrap function that runs before 14 | * your application gets started. 15 | * 16 | * This gives you an opportunity to set up your data model, 17 | * run jobs, or perform some special logic. 18 | */ 19 | bootstrap(/*{ strapi }*/) {}, 20 | }; 21 | --------------------------------------------------------------------------------