Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-#249: Now authenticate users able to create blog post. #250

Merged

Conversation

0xmohitsen
Copy link
Contributor

@0xmohitsen 0xmohitsen commented May 15, 2024

Summary

This PR allow authenticated users to create blog posts

Description

This PR addresses an issue where authenticated users were unable to create blog posts despite having valid and unexpired tokens. The problem manifested as an error toast displaying "Something went wrong. Please try again later!" when authenticate users attempted to create a post.

Now authenticate users can create blog post until and unless tokens are valid and unexpired. And if tokens get expired or invalid, we simply display toastify "Your session has expired, please login again!" and redirects to homepage with conditional rendering.

Issue(s) Addressed

Closes #249

Prerequisites

…user from userState when tokens gets expired + correction in error toastify
Copy link

vercel bot commented May 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wanderlust ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2024 6:49pm
wanderlust-backend ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2024 6:49pm

Copy link
Contributor

@Sukomal07 Sukomal07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use toast.promise to show different states (pending, success, error) of the API request.

@0xmohitsen
Copy link
Contributor Author

Can you use toast.promise to show different states (pending, success, error) of the API request.

Sure.

@krishnaacharyaa
Copy link
Owner

@0xmohitsen @Sukomal07 kindly fix this
#254 is blocked because of this

@Sukomal07
Copy link
Contributor

Ok

@Sukomal07
Copy link
Contributor

Sukomal07 commented May 16, 2024

@0xmohitsen change your code in this way

`
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
if (validateFormData()) {
try {
const response = axiosInstance.post('/api/posts/', formData);
toast.promise(response, {
pending: 'Creating blog ...',
success: {
render() {
setFormData({
title: '',
authorName: '',
imageLink: '',
categories: [],
description: '',
isFeaturedPost: false,
})
navigate('/')
return "Blog created successfully"
},
},
error: {
render({ data }) {
if (data instanceof AxiosError) {
if (data?.response?.data?.message) {
return data?.response?.data?.message;
}
}
return "Blog created failed"
},
},
}
)

    return (await response).data

  } catch (error) {
    if (isAxiosError(error)) {
      console.error(error.response?.data?.message);
    } else {
      console.error(error);
    }
  }
}

};
`

@0xmohitsen
Copy link
Contributor Author

@0xmohitsen change your code in this way

` const handleSubmit = async (e: FormEvent) => { e.preventDefault(); if (validateFormData()) { try { const response = axiosInstance.post('/api/posts/', formData); toast.promise(response, { pending: 'Creating blog ...', success: { render() { setFormData({ title: '', authorName: '', imageLink: '', categories: [], description: '', isFeaturedPost: false, }) navigate('/') return "Blog created successfully" }, }, error: { render({ data }) { if (data instanceof AxiosError) { if (data?.response?.data?.message) { return data?.response?.data?.message; } } return "Blog created failed" }, }, } )

    return (await response).data

  } catch (error) {
    if (isAxiosError(error)) {
      console.error(error.response?.data?.message);
    } else {
      console.error(error);
    }
  }
}

}; `

Added this code and tested.

@krishnaacharyaa krishnaacharyaa merged commit f472c18 into krishnaacharyaa:main May 16, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Authenticate user is not able to create blog post.
3 participants