From the last article, we concluded user registeration and authentication flow. It was surreal to me and I hope you find it intriguing too. In this article (possibly the last in this series), we'll look at how authenticated users can update their details.
Source code
The overall source code for this project can be accessed here:
This project was deployed on heroku (backend) and vercel (frontend) and its live version can be accessed here.
To run this application locally, you need to run both the backend and frontend projects. While the latter has some instructions already for spinning it up, the former can be spinned up following the instructions below.
This project was deployed on heroku (backend) and vercel (frontend) and its live version can be accessed here.
Notabene
The project's file structure has considerably been modified from where we left off. Also, most of the scripts have be re-written in TypeScript. The concept of SvelteKit environment variables, TypeScript's interfaces, powerful loader, and a host of others were also implemented. We now have the following file structure for the frontend project:
Now, let's get right into adding this functionality.
Update user data
It's a very common thing in web applications to allow users alter their initial data. Let's provide this feature our application's users too.
Create a .svelte file in routes/accounts/user/ directory. You are at liberty to give it any name you want. However, I'd like to make it dynamic. To make a dynamic page routing in SvelteKit, you use [](square brackets) with the dynamic field inside and then .svelte. For our purpose, we want the URL to have user's username and ID. Therefore, the name of our dynamic file will be [username]-[id].svelte. Awesome huh! SvelteKit is truly awesome.
Next, let's purpulate this newly created file with the following content:
<scriptcontext="module"lang="ts">import{variables}from'$lib/utils/constants';import{getCurrentUser}from'$lib/utils/requestUtils';importtype{Load}from'@sveltejs/kit';importtype{User}from'$lib/interfaces/user.interface';exportconstload:Load=async ({fetch})=>{const[userRes,errs]=awaitgetCurrentUser(fetch,`${variables.BASE_API_URI}/token/refresh/`,`${variables.BASE_API_URI}/user/`);constuserResponse:User=userRes;if (errs.length>0&&!userResponse.id){return{status:302,redirect:'/accounts/login'};}return{props:{userResponse}};};</script>
<scriptlang="ts">import{notificationData}from'$lib/store/notificationStore';import{scale}from'svelte/transition';import{UpdateField}from'$lib/utils/requestUtils';import{onMount}from'svelte';import{nodeBefore}from'$lib/helpers/whitespacesHelper';exportletuserResponse:User;consturl=`${variables.BASE_API_URI}/user/`;onMount(()=>{constnotifyEl=document.getElementById('notification')asHTMLElement;if (notifyEl&&$notificationData!==''){setTimeout(()=>{notifyEl.classList.add('disappear');notificationData.update(()=>'');},3000);}});lettriggerUpdate=async (e:Event)=>{constsibling=nodeBefore(<HTMLElement>e.target);awaitUpdateField(sibling.name,sibling.value,url);};</script>
<divclass="container"transition:scale|local={{start:0.7,delay:500}}>{#ifuserResponse.id}<h1>{userResponse.full_name?userResponse.full_name:userResponse.username}profile</h1>
{/if}
<divclass="user"transition:scale|local={{start:0.2}}><divclass="text"><inputaria-label="User's full name"type="text"placeholder="User's full name"name="full_name"value={userResponse.full_name}/>
<buttonclass="save"aria-label="Save user's full name"on:click={(e)=>triggerUpdate(e)}/>
</div>
</div>
<divclass="user"transition:scale|local={{start:0.3}}><divclass="text"><inputaria-label="User's username"type="text"placeholder="User's username"name="username"value={userResponse.username}/>
<buttonclass="save"aria-label="Save user's username"on:click={(e)=>triggerUpdate(e)}/>
</div>
</div>
<divclass="user"transition:scale|local={{start:0.4}}><divclass="text"><inputaria-label="User's email"placeholder="User's email"type="email"name="email"value={userResponse.email}/>
<buttonclass="save"aria-label="Save user's email"on:click={(e)=>triggerUpdate(e)}/>
</div>
</div>
<divclass="user"transition:scale|local={{start:0.5}}><divclass="text"><inputaria-label="User's bio"placeholder="User's bio"type="text"name="bio"value={userResponse.bio}/>
<buttonclass="save"aria-label="Save user's bio"on:click={(e)=>triggerUpdate(e)}/>
</div>
</div>
<divclass="user"transition:scale|local={{start:0.6}}><divclass="text"><inputaria-label="User's date of birth"type="date"name="birth_date"placeholder="User's date of birth"value={userResponse.birth_date}/>
<buttonclass="save"aria-label="Save user's date of birth"on:click={(e)=>triggerUpdate(e)}/>
</div>
</div>
</div>
Whoa!!! That's a lot, man! Errm... It's but let's go through it.
Module script section: We started the file by creating a script module. Inside it is the magical load function which does only one thing: get the current user. Were you successful at that? Yes? Put the response in userResponse variable and make it available to the rest of the program using props. No? Redirect the user to the login page. Pretty simple huh? I think it's.
Second script section: This section's snippets are pretty basic. The major things to note are the retrieval of the props made available by our module, and the definition of triggerUpdate asynchronous function. To retrieve and then expose props values, we only did export let userResponse: User; and that's it. What about the triggerUpdate function? Well, it is a very short function with this definition:
It accepts an Event object, and using it, determines the value and name of the previous sibling (an input) using a custom function, named nodeBefore. Why not use (<HTMLElement>e.target).previousSibling instead? This MDN article, How whitespace is handled by HTML, CSS, and in the DOM, explained it. As a matter of fact, the snippets in $lib/helpers/whitespacesHelper.ts were ported from the JavaScript snippets made available on the article. Then, we called on UpdateField function, having this content:
// lib -> utils -> requestUtils.ts...exportconstUpdateField=async (fieldName:string,fieldValue:string,url:string):Promise<[object,Array<CustomError>]>=>{constuserObject:UserResponse={user:{}};letformData:UserResponse|any;if (url.includes('/user/')){formData=userObject;formData['user'][`${fieldName}`]=fieldValue;}else{formData[`${fieldName}`]=fieldValue;}const[response,err]=awaithandlePostRequestsWithPermissions(fetch,url,formData,'PATCH');if (err.length>0){console.log(err);return[{},err];}console.log(response);notificationData.set(`${formatText(fieldName)} has been updated successfully.`);return[response,[]];};
This function just prepares the data to be sent to the server and then calls on the function that really sends it: handlePostRequestsWithPermissions. handlePostRequestsWithPermissions is a multipurpose or maybe generic function that can be used to make any post requests that require some permissions. Though written to work for this project, it can be modified to suit other projects' needs. It's content is:
It currently handles POST and PATCH requests but as said earlier, it can be extended to accommodate PUT, DELETE, and other "unsafe" HTTP verbs.
The triggerUpdate method was bound to the click event of the button element attached to each input element on the form. When you focus on the input element, a disk-like image pops up at right-most part the the input and clicking it triggers triggerUpdate which in-turn calls on updateField, and then handlePostRequestsWithPermissions.
[Heaves a sigh of relief], that's basically it! If I get less busy, I might still work on this project to make it more than just an authentication system. Contributions are also welcome. Kindly drop comments if there's anything you wanna let me know. See y'all...