@@ -140,19 +140,64 @@ public function generateToken($tokenLifetime): string
140140 $ userIdKey = Config::get ('id.key ' );
141141 $ secretPhrase = Config::get ('token.secret ' );
142142
143- // no fallback because we need the user id
144- $ userId = $ this ->data [$ userIdKey ];
145-
146143 $ payload = [
147- 'user.id ' => $ userId ,
144+ 'user.id ' => $ this -> data [ $ userIdKey ] ,
148145 'iat ' => time (),
149146 'exp ' => $ tokenLifetime ,
150147 'iss ' => $ _SERVER ['HTTP_HOST ' ] ?? 'localhost ' ,
151148 ];
152149
153- $ token = JWT ::encode ($ payload , $ secretPhrase , 'HS256 ' );
150+ return JWT ::encode ($ payload , $ secretPhrase , 'HS256 ' );
151+ }
152+
153+ /**
154+ * Generate a verification token for the user
155+ * @param mixed $expiresIn Token expiration time
156+ * @return string
157+ */
158+ public function generateVerificationToken ($ expiresIn = null ): string
159+ {
160+ $ userIdKey = Config::get ('id.key ' );
161+ $ secretPhrase = Config::get ('token.secret ' ) . '-verification ' ;
162+
163+ $ payload = [
164+ 'user.id ' => $ this ->data [$ userIdKey ],
165+ 'user.email ' => $ this ->data ['email ' ],
166+ 'iat ' => time (),
167+ 'exp ' => $ expiresIn ?? (time () + 600 ),
168+ 'iss ' => $ _SERVER ['HTTP_HOST ' ] ?? 'localhost ' ,
169+ ];
170+
171+ return JWT ::encode ($ payload , $ secretPhrase , 'HS256 ' );
172+ }
173+
174+ /**
175+ * Check if email is verified
176+ * @return bool
177+ */
178+ public function isVerified (): bool
179+ {
180+ return !!$ this ->data ['email_verified_at ' ];
181+ }
154182
155- return $ token ;
183+ /**
184+ * Verify user's email
185+ * @return bool
186+ */
187+ public function verifyEmail (): bool
188+ {
189+ $ this ->data ['email_verified_at ' ] = tick ()->format (Config::get ('timestamps.format ' ));
190+
191+ try {
192+ $ this ->db ->update (Config::get ('db.table ' ))
193+ ->params (['email_verified_at ' => $ this ->data ['email_verified_at ' ]])
194+ ->where (Config::get ('id.key ' ), $ this ->data [Config::get ('id.key ' )])
195+ ->execute ();
196+
197+ return true ;
198+ } catch (\Throwable $ th ) {
199+ return false ;
200+ }
156201 }
157202
158203 public function get ()
0 commit comments