After the process has changed user id, it is no longer permitted to change group id. Simply swap the two, like this:
if ( setgid((*user).pw_gid) == -1) {
printf("Error %d setting group ID to %d\n", errno, (*user).pw_gid);
return 1;
}
if ( setuid((*user).pw_uid) == -1) {
printf("Error %d setting user ID to %d\n", errno, (*user).pw_uid);
return 1;
}