Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Kezios profile imageJustin Martin
Justin Martin forKezios

Posted on • Edited on • Originally published atkezios.fr

     

React.js - Indicateur de "force" d'un mot de passe

Pour sécuriser les mots de passe, on demande souvent à l’utilisateur d’utiliser un mot de passe “complexe” en ajoutant par exemple une majuscule, un caractère spécial ou un chiffre. Cependant, ces méthodes ne permettent pas de créer un mot de passe aussi sécurisé qu’on le pense.

Par exemple, le mot de passe:[Password1234] réponds à toutes ces règles, cependant il est l’un des plus testés lors des attaques.

Comment faire alors ?

L’une des solutions pour créer un processus de validation de mot de passe fiable est d’utiliser une librairie commezxcvbn-ts (https://zxcvbn-ts.github.io/zxcvbn/). Cette librairie permet d’estimer la complexité d’un mot de passe et ainsi d’empêcher l’utilisateur d’utiliser un mot de passe jugé trop faible.

Implémentation en React.js

L’idée est de calculer le score du mot de passe indiqué par l’utilisateur et en fonction de ce score affiché les indicateurs qui correspondent :

Indicateur de mot de passe

App.tsx :

importReact,{useEffect,useState}from"react";import{zxcvbn}from"@zxcvbn-ts/core";import{Indicators}from"./components/Indicators";import"./App.css";interfaceIndicator{score:number;}constApp=()=>{const[password,setPassword]=useState("");const[indicator,setIndicator]=useState<Indicator>({score:-1});useEffect(()=>{if(password==="")return;setIndicator(zxcvbn(password));},[password]);const{score}=indicator;return(<divclassName="d-block mx-4"><divclassName="position-relative mt-3"><labelhtmlFor="password-input"className="mr-2">          Mot de passe</label><inputis="password-input"type="password"onChange={(event)=>setPassword(event.target.value)}value={password}placeholder={"**********"}/>{password!==""&&<Indicatorsscore={score}/>}</div></div>);};exportdefaultApp;
Enter fullscreen modeExit fullscreen mode

Indicators.tsx

importReactfrom"react";constcolors={0:"#e5e5e5",1:"#9B2C2C",2:"#D44949",3:"#DCA02D",4:"#387F95",5:"#48AE65"};constgetColor=(power,index)=>{if(power>index){returncolors[power];}returncolors[0];};constindicatorIndexes=[0,1,2,3,4];constIndicators=({score}:{score:number})=>(<divclassName="mt-2 indicator-container">{indicatorIndexes.map((indicatorIndex,index)=>(<divclassName="indicator"key={indicatorIndex}style={{backgroundColor:getColor(score+1,indicatorIndex)}}/>))}</div>);export{Indicators};
Enter fullscreen modeExit fullscreen mode

App.css

.indicator{height:4px;border-radius:4px;width:15%;margin-right:8px;}.indicator-container{flex-direction:row;display:flex;}
Enter fullscreen modeExit fullscreen mode

Pour allez plus loin

A présent, on va ajouter des options dans notre validation. Avec pour objectif pour rendre notre validation de mot de passes plus sécurisé. On va aussi ajouter des suggestions pour indiquer à l’utilisateur comment rendre son mot de passe plus fort.

Indicateur de mot de passe avec suggestions

App.tsx :

importReact,{useEffect,useState}from"react";import{zxcvbn,ZxcvbnOptions}from"@zxcvbn-ts/core";importzxcvbnCommonPackagefrom"@zxcvbn-ts/language-common";importzxcvbnFrPackagefrom"@zxcvbn-ts/language-fr";import{FeedbackType}from"@zxcvbn-ts/core/dist/types";import{Indicators}from"./components/Indicators";import{Suggestions}from"./components/Suggestions";import"./App.css";constoptions={translations:zxcvbnFrPackage.translations,graphs:zxcvbnCommonPackage.adjacencyGraphs,dictionary:{...zxcvbnCommonPackage.dictionary,...zxcvbnFrPackage.dictionary}};ZxcvbnOptions.setOptions(options);interfaceIndicator{score:number;feedback:FeedbackType;}constApp=()=>{const[password,setPassword]=useState("");const[indicator,setIndicator]=useState<Indicator|null>();useEffect(()=>{if(password==="")return;setIndicator(zxcvbn(password));},[password]);constscore=indicator?indicator.score:-1;constfeedback=indicator?indicator.feedback:undefined;return(<divclassName="d-block mx-4"><divclassName="position-relative mt-3"><labelhtmlFor="password-input"className="mr-2">          Mot de passe</label><inputis="password-input"type="password"onChange={(event)=>setPassword(event.target.value)}value={password}placeholder={"**********"}/>{password!==""&&<Indicatorsscore={score}/>}{feedback&&feedback.warning.length>0&&(<Suggestionssuggestions={feedback.suggestions}/>)}</div></div>);};exportdefaultApp;
Enter fullscreen modeExit fullscreen mode

Suggestions.tsx

importReactfrom"react";constSuggestions=({suggestions}:{suggestions:string[]})=>(<ul>{suggestions.map((suggestion,index)=>(<likey={suggestion}>{suggestion}</li>))}</ul>);export{Suggestions};
Enter fullscreen modeExit fullscreen mode

Lien codesandbox:

https://codesandbox.io/s/password-zxcvbn-react-ts-3lt0q

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Il n'y a pas de bons services sans bonnes technologies

More fromKezios

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp