@malevich-studio/strapi-sdk-typescript 1.2.3 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -108,13 +108,41 @@ const articles = api.articles(
108
108
  );
109
109
  ```
110
110
 
111
+ ### Login Example
112
+
113
+ Next.js
114
+
115
+ ```ts
116
+ 'use server';
117
+
118
+ import Strapi from '@/strapi';
119
+ import { cookies } from 'next/headers';
120
+
121
+ export async function login(email: string, password: string) {
122
+ const strapi = new Strapi(process.env.STRAPI_URL || '');
123
+ const response = await strapi.login(email, password);
124
+
125
+ if (!response.error) {
126
+ (await cookies()).set('access_token', response.jwt, {
127
+ httpOnly: true,
128
+ secure: process.env.NODE_ENV === 'production',
129
+ sameSite: 'lax',
130
+ path: '/',
131
+ maxAge: 3600 * 24 * 365 * 10,
132
+ });
133
+ }
134
+
135
+ return response;
136
+ }
137
+ ```
138
+
111
139
  ## 📌 TODO List
112
140
 
113
141
  - [ ] Add authentication features:
114
- - [ ] Log In functionality
115
- - [ ] User Registration
142
+ - [x] Log In functionality
143
+ - [x] User Registration
116
144
  - [ ] User privileges check
117
- - [ ] Add localization features
145
+ - [x] Add localization features
118
146
  - [ ] Refactor `src/generator/index.ts` for better maintainability
119
147
  - [ ] Enable passing Strapi credentials via CLI parameters
120
148
  - [ ] Allow customization of API class path
package/dist/cli.cjs CHANGED
@@ -20122,6 +20122,9 @@ class Strapi {
20122
20122
  setToken(token) {
20123
20123
  this.token = token;
20124
20124
  }
20125
+ getToken() {
20126
+ return this.token;
20127
+ }
20125
20128
  async baseLogin(identifier, password) {
20126
20129
  const response = await this.fetchData('/auth/local', {
20127
20130
  identifier,
@@ -20129,8 +20132,55 @@ class Strapi {
20129
20132
  }, {
20130
20133
  method: 'POST',
20131
20134
  });
20132
- this.setToken(response.jwt);
20133
- return response.user;
20135
+ if (!response.error) {
20136
+ this.setToken(response.jwt);
20137
+ }
20138
+ return response;
20139
+ }
20140
+ async baseRegister(data) {
20141
+ const response = await this.fetchData('/auth/local/register', data, {
20142
+ method: 'POST',
20143
+ });
20144
+ if (!response.error) {
20145
+ this.setToken(response.jwt);
20146
+ }
20147
+ return response;
20148
+ }
20149
+ async forgotPassword(email) {
20150
+ return await this.fetchData('/auth/forgot-password', { email }, {
20151
+ method: 'POST',
20152
+ });
20153
+ }
20154
+ async sendEmailConfirmation(email) {
20155
+ return await this.fetchData('/auth/send-email-confirmation', { email }, {
20156
+ method: 'POST',
20157
+ });
20158
+ }
20159
+ async baseResetPassword(password, code) {
20160
+ const response = await this.fetchData('/auth/reset-password', {
20161
+ password,
20162
+ passwordConfirmation: password,
20163
+ code,
20164
+ }, {
20165
+ method: 'POST',
20166
+ });
20167
+ if (!response.error) {
20168
+ this.setToken(response.jwt);
20169
+ }
20170
+ return response;
20171
+ }
20172
+ async baseChangePassword(password, currentPassword) {
20173
+ const response = await this.fetchData('/auth/change-password', {
20174
+ password,
20175
+ passwordConfirmation: password,
20176
+ currentPassword,
20177
+ }, {
20178
+ method: 'POST',
20179
+ });
20180
+ if (!response.error) {
20181
+ this.setToken(response.jwt);
20182
+ }
20183
+ return response;
20134
20184
  }
20135
20185
  async getLocales(params) {
20136
20186
  return await this.baseFetch('i18n/locales', _.merge({
@@ -20751,7 +20801,19 @@ async function generateStrapiTypes(strapi) {
20751
20801
  ` public async login(identifier: string, password: string) {`,
20752
20802
  ` return await this.baseLogin<User>(identifier, password);`,
20753
20803
  ' }',
20754
- '',
20804
+ ' ',
20805
+ ` public async register(data: UserInput) {`,
20806
+ ` return await this.baseRegister<User, UserInput>(data);`,
20807
+ ' }',
20808
+ ' ',
20809
+ ` public async resetPassword(password: string, code: string) {`,
20810
+ ` return await this.baseResetPassword<User>(password, code);`,
20811
+ ' }',
20812
+ ' ',
20813
+ ` public async changePassword(password: string, currentPassword: string) {`,
20814
+ ` return await this.baseChangePassword<User>(password, currentPassword);`,
20815
+ ' }',
20816
+ ' ',
20755
20817
  methods.join('\n\n'),
20756
20818
  '}',
20757
20819
  '',