Nanfeng

Notes on software development, code, and curious ideas

Cocos Creator Daily First Login Detection with localStorage

Store the most recent login date locally, compare it with today, and update it when they differ:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const LAST_LOGIN_KEY = 'lastLoginDate';

function isFirstLoginToday(): boolean {
const currentDate = new Date().toDateString();
const lastLoginDate = cc.sys.localStorage.getItem(LAST_LOGIN_KEY);

if (lastLoginDate !== currentDate) {
cc.sys.localStorage.setItem(LAST_LOGIN_KEY, currentDate);
return true;
}
return false;
}

if (isFirstLoginToday()) {
console.log('First login today: grant the reward.');
} else {
console.log('The player already logged in today.');
}

This is device-local state, so it can be reset by clearing storage and does not synchronize between devices. Use a trusted server date for authoritative rewards.

+